Im new to PHP and still learning, but I want to know if I am able to do something like this. I have a basic HTML form like the one below.
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
</form>
And a basic upload PHP document like the one below as well.
<?php
if ($_FILES["file"]["size"] < 2097152) {
if ($_FILES["file"]["error"] > 0) {
header('Location: /');
}else {
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
if (file_exists($filepath)) {
echo $filepath . " already exists. ";
}else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}else {
echo "File too large.";
}
?>
What i'm asking is, is it possible to have a checkbox on the HTML form that when checked the file will upload to a folder named "private" and when unchecked the file will upload to a folder named "public". Thank you.
You can use the condition like this so that than only you can get the checkbox value after the form is submitted.
if(isset($_POST['check']))
{
$filepath = "upload/private" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/public" . $randprefix . $_FILES["file"]["name"];
}
You have to place the name for the checkbox input type so that you can check in the upload.php page and then set the folder over there.
Replace:
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
With:
<label><input type="checkbox" id="cb" value="checkb" name="check">Make Private.</label>
Explanation: When the user selects the checkbox at the time of submitting it will check for the check box value and it will make the $filepath as upload/private or else if the user while submitting the data he/she has not selected the check box it will change the code as per the selection if the check box value. So that you can move_upload_file() based on the selection available.
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" name="checkb" value="checkb">Make Private.</label>
</form>
<?php
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
header('Location: /');
}
else
{
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
if($_POST['checkb'] == "checkb")
{
$filepath = "private/" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
}
if (file_exists($filepath))
{
echo $filepath . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '' . $filepath . '' ;
}
}
}
else
{
echo "File too large.";
}
?>
I think it is work I test It and you haven`t problems to copy!
Related
Hi so I have this upload script, that can upload one file at the time but I need to upload more than one file at the same time and then name every uploaded file as 1 and the next file as 2 and the next one as 3 and so on.
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png","JPG");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1032) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("i/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "i/" . $_FILES["file"]["name"]);
echo "Stored in: " . "i/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Just use several inputs on your html:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
And in your PHP
<?php
for ($i=0; $i < count($_FILES['name']); $i++) {
$new_file = $_FILES['name'][$i] . '.' . $i+1;
}
?>
The later will create the filenames appending the index +1 so you'll have:
myfile1.txt.1
myfile1.txt.2
Adjust as needed.
Edit: Check this link.
You should be able to do like this:
$files = array();
$allowed_filetypes = array('jpg','jpeg','gif','bmp','png','tif','pdf');
$max_filesize = 15242880;
$upload_path = 'images/image_uploads/';
for ($i = 0; $i < count($_FILES['files']['name']); $i++){
if($_FILES['files']['name'][$i] != "") {
$filename = $_FILES['files']['name'][$i];
$file_info = pathinfo($_FILES['files']['name'][$i]);
$ext = strtolower($file_info['extension']);
//echo "<span style='font-size: 20px; color: #ff3333;'>".$ext."</span>";
if(!in_array($ext,$allowed_filetypes))
die("The file you attempted to upload ($filename) is not allowed.");
if(filesize($_FILES['files']['tmp_name'][$i]) > $max_filesize)
die("The file you attempted to upload ($filename) is too large.");
if(!is_writable($upload_path))
die("You cannot upload to the specified directory, please CHMOD it to 777.");
$filename = ($i+1).".".$ext;
if(move_uploaded_file($_FILES['files']['tmp_name'][$i],$upload_path.$filename)) {
$result = mysql_query("Insert Into image_uploads_images (upload_id, image, original_name) Values ('$id', '$filename', '".$_FILES['files']['name'][$i]."');");
if($result){
array_push($files, "http://www.site.com/images/image_uploads/$filename => ".$_FILES['files']['name'][$i]);
}else{
echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
}
}else{
echo "<p style=\"color:#cc3333;\">Unable to upload ".$_FILES['files']['name'][$i]."</p>";
}
}
}
Edit
Just as a brief explanation, all file inputs would have the same name, in this case files. But when you do that, you need to add [] behind the name in the form element, like <input type="file" name="files[]" /> so that it comes through as an array.
Once you have the $_FILES array you can go through it recursively for each file uploaded and do with it what you need.
I am using xampp on Windows 7. My problem is that the image isn't appearing when I clicked the submit button.
Here's my php code:
<?php
$fileLocation = 'C:\xampp\htdocs\images';
$name = $_FILES["file"]["name"];
$tempName = $_FILES["file"]["tmp_name"];
$size = $_FILES["file"]["size"];
$fileType = $_FILES["file"]["type"];
if ($fileType == "images/jpeg" && $size < 2000000)
{
if ($_FILES['file']['error'] > 0)
{
echo "File error! : " . $_FILES['file']['error']. "</br>";
}
else
{
echo "File Name: " . $name . "</br>";
echo "File Type: " . $fileType . "</br>";
echo "File Size: " . $size . "</br>";
}
if (file_exists($file_location, $name))
{
echo "File already exists!" . $name . "</br>";
}
else
{
move_uploaded_file($tempname, $fileLocation.$name);
echo "Stored in: " . $fileLocation . "</br>";
}
}
?>
HTML CODE:
<form enctype="multipart/form-data" action="uploadFile.php" method="POST">
Send this file: <input name="file" type="file" id='file'/>
<input type="submit" value="Send File" name="submit"/>
</form>
Thank you for your response.
Also, check if Your HTML form has enctype='multipart/form-data' attribute set. It's easy to forget about it (at least I do almost every time).
In PHP, is it possible to upload files to the server? If so, then how can I create an upload form and insert the files to a specific page? Any extension images: .jpeg .gif or files: .txt .pdf etc. I am using XAMPP (localhost to view my website).
I have tried many tutorials, but there are lot of errors.
This is this HTML file:
<form action="fileupload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
And here is the PHP script:
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
The result is shown like this:
Upload: (file here)
Type: image/jpeg
Size: 757.521484375 Kb
Stored in: (temp file in C:)
But whenever I find my file in the tmp folder. It's nowhere to be found.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$extension = getExtension($filename);
$extension = strtolower($extension);
if($extension=="jpg" || $extension=="jpeg" || $extension=="gif" || $extension=="txt" || $extension=="pdf" ){
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" .date("d_m_h_i_s"). $_FILES["file"]["name"]);
}
The getExtension function helps to get the extension of the file. The files will be uploaded to the upload folder.
My html form:
<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
My php file:
if ($_POST['submit'] == "Submit") {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
}
}
there are 2 folders in my public_html: tmp and downloads, both 777 permissions (just to test)
this file is located in a .htaccess protected folder within downloads (public_html/downloads/new/update.php)
and i want the zip files to be uploaded in the downloads dir.
This code won't give me any errors, but does not upload the file. Why?
Try adding
if(move_uploaded_file(...)){
echo "it works";
} else {
echo "NOPE";
}
And
replace:
move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);
with:
move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
PHP needs MAX_FILE_SIZE to receive uploaded files
<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
with this form you can upload a zip archiv with 10 MB or less...
you must also set the max_post_size and upload_max_filesize in your php.ini to the same or a higher value to upload files
use this PHP code
if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/downloads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(move_uploaded_file($_FILES["file"]["tmp_name"],
"/downloads/" . $_FILES["file"]["name"]))
echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
else echo "file could not be processed";
}
}
This is how i solved it:
HTML:
<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if($_POST['submit'] == "Submit") {
$filename = $_FILES["file"]["name"];
$source = $_FILES["file"]["tmp_name"];
$type = $_FILES["file"]["type"];
$name = explode(".", $filename);
$target_path = "../".$filename;
if(move_uploaded_file($source, $target_path)) {
$message = "Your .zip file was uploaded";
} else {
$message = "ERROR";
}
if($message) echo $message;
}
i simply want that after i upload an image it will be redirected to a page that displays all the images. The images are saved in a folder. i have found some examples but they are so complicated, i just want to try the simple way first.
this is the HTML code for uploading image:
<form action="upload_image.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
Title:<input type="text" name="title" id="title" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
this is the PHP code to save the other details in the database:
include('../IFM-mobile_website/include/connect.php');
$title=$_POST['title'];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"]) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
any help would be much appreciated.
Your file uploaded path and the path in which you are showing image are different
uploading to : "upload/" . $_FILES["file"]["name"]
showing it from : "../IFM-mobile_website/upload/" . $_FILES["file"]["name"]
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
To show all the images in a folder :
$a = glob('Your/path/*.{jpg,gif,png}',GLOB_BRACE);
print_r($a);