My PHP upload script is not getting anything from my form. Here is my form:
<small>Must be JPG, max 200kb</small>
<input id="image" type="checkbox" name="image" value="yes">
<input type="file" name="file" id="file">
Here is the section of PHP that handles the upload, I know that $image does infact = 'yes', so it looks like the problem is with the $_FILES array, as $_FILES["file"]["size"] is empty:
if ($image=="yes" && $_FILES["file"]["size"]>0) {
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
$picstuff="Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
$picstuff= "Upload: " . $_FILES["file"]["name"] . "<br />
Type: " . $_FILES["file"]["type"] . "<br />
Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /><br><br>
<center>Please note it can take a couple of minutes for the image to be processed.</center><br><br>";
move_uploaded_file($_FILES["file"]["tmp_name"], "images/$id.jpg");
$query="UPDATE content SET image='yes' WHERE `id`='$id'";
$result=mysql_query($query);
}
}
else
{
$picstuff = "Image too large or incorrect format. Please upload a jpeg less than 200kb.";
$image= "no";
$query="UPDATE content SET image='No' WHERE `id`='$id'";
$result=mysql_query($query);
}
} else
{
$picstuff = 'No File found';
$image= "no";
$query="UPDATE content SET image='No' WHERE `id`='$id'";
$result=mysql_query($query);
};
Every time I try upload I just get the 'No File found'.
Would appreciate any help!
I put enctype="multipart/form-data" into my form tag and it fixed my problem.
Related
upload_file.php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma", "MP4");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/wma")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
&& ($_FILES["file"]["size"] < 20000000)
&& 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"] / 1024) . " 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: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
html
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
So my problem is I get the message "Invalid File". This only happens when I try to upload video type files. However when I try to upload an image it works like a charm. I've searched all over stackoverflow for other video file upload codes and still couldn't find any that worked. Anyone that could refer another question/solution to me and/or fix this problem will greatly be appreciated.
EXTRA NOTE
I've already tried adding echo "Its type is " . $_FILES["file"]["type"]; to debug what file type is being given however it just returns a nice white space.
Change this part
else
{
echo "Invalid file";
}
to
else
{
echo "Invalid file";
echo "Its type is " . $_FILES["file"]["type"];
}
Now upload the files that don't work and add those types to your list
Apparently the problem only was because the php.ini was set to only accept 10M the file I was uploading was over 15MB and so I guess it gave me the error. But shouldn't that give me the error file-size is too much or something? But that's basically the reason I got the error. :)
I am new to php,trying to do a form, which allows me to upload a few text fields and then upload 3 images, the images are then uploaded into a remote server, and the name of the images are saved into database, so then it can be pulled out and displayed later.
(p.s. Ignore the SQL injection issue, i just havent got time around that yet. Thanks)
At the moment i am testing this on localhost.
My question is should i loop it, or should i just do the upload process 3 times?
Below is what i tried. But the looping trial isnt uploading the files, it just goes straight to invalid file. Also i would like to make sure all 3 files are valid files before uploading them, would i have to loop the verifying first then loop the uploading again?
Please give me some advice, thanks for your time.
<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';
if ($_POST)
{
//get form data
$Listingname = addslashes(strip_tags($_POST['Listingname']));
$Location = addslashes(strip_tags($_POST['Location']));
$nobed = addslashes(strip_tags($_POST['nobed']));
$zip = addslashes(strip_tags($_POST['zip']));
$price = addslashes(strip_tags($_POST['price']));
$username=addslashes(strip_tags($_POST[$_SESSION['username']]));
if (!$Listingname||!$nobed||!$nobed||!$zip||!$price)
echo "Please fill out all fields";
else
for($i=0;$i<count($_FILES["image"]["name"]);$i++)
{$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 400000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"][$i] . "<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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"][$i] . " already exists please add another file, or change the. ";
}
else
{
$photo=$_FILES["file"]["name"][$i];
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/$photo");
echo "Stored in: " . "upload/" . $_FILES["file"]["name"][$i];
}
}
}
else
{
echo "Invalid file";
}
{
$username=$_SESSION['username'];
//register into database
mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES
('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error());
echo "Listing Added";
}
}
}
else
{
?>
<form action="Submitlisting2.php" method="post"
enctype="multipart/form-data">
Listing Name:<br />
<input type='text' name='Listingname'><p />
Location:<br />
<input type='text' name='Location'><p />
Number of Beds:<br />
<input type='test' name='nobed'><p />
Zip:<br />
<input type='text' name='zip'><p />
Price:<br />
<input type='text' name='price'><p />
<label for="file">Pic1(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file"><br>
<label for="file">Pic2(File must be exceed 4mb):</label>
<input type="file" name="file[]" id="file"><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
}
?>
Attempt without looping(this get very very long, the attempt is only with 2 images upload, which is 1 reason why i am thinking to loop)
<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';
if ($_POST)
{
//get form data
$Listingname = addslashes(strip_tags($_POST['Listingname']));
$Location = addslashes(strip_tags($_POST['Location']));
$nobed = addslashes(strip_tags($_POST['nobed']));
$zip = addslashes(strip_tags($_POST['zip']));
$price = addslashes(strip_tags($_POST['price']));
$username=addslashes(strip_tags($_POST[$_SESSION['username']]));
if (!$Listingname||!$nobed||!$nobed||!$zip||!$price)
echo "Please fill out all fields";
else
{$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 400000)
&& 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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
}
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists please add another file, or change the. ";
}
}
}
}
else
{$allowedExts1 = array("gif", "jpeg", "jpg", "png");
$temp1 = explode(".", $_FILES1["file1"]["name1"]);
$extension1 = end($temp1);
if ((($_FILES1["file"]["type"] == "image/gif")
|| ($_FILES1["file"]["type"] == "image/jpeg")
|| ($_FILES1["file"]["type"] == "image/jpg")
|| ($_FILES1["file"]["type"] == "image/pjpeg")
|| ($_FILES1["file"]["type"] == "image/x-png")
|| ($_FILES1["file"]["type"] == "image/png"))
&& ($_FILES1["file"]["size"] < 400000)
&& in_array($extension1, $allowedExts1))
{
if ($_FILES1["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES1["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES1["file"]["name"] . "<br>";
echo "Type: " . $_FILES1["file"]["type"] . "<br>";
echo "Size: " . ($_FILES1["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES1["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES1["file"]["name"]))
{
echo $_FILES1["file"]["name"] . " already exists please add another file, or change the. ";
}
}
}
else
{ $photo=$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/$photo");
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
else
{
echo "Pic 1 Invalid file" and die("Unable to upload pic1");
}
}
{
$photo1=$_FILES1["file"]["name"];
move_uploaded_file($_FILES1["file"]["tmp_name"],
"upload/$photo1");
echo "Stored in: " . "upload/" . $_FILES1["file"]["name"];
}
else
{
echo "Pic 2 Invalid file" and die("Unable to upload Pic2");
}
{
$username=$_SESSION['username'];
//register into database
mysqli_query($con,"INSERT INTO Listing (username,Listingname,Location,nobed,zip,price,pic1) VALUES
('$username','$Listingname','$Location','$nobed','$zip','$price','$photo');") or die(mysqli_error());
echo "Listing Added";
}
else
{
?>
<form action="Submitlisting2.php" method="post"
enctype="multipart/form-data">
Listing Name:<br />
<input type='text' name='Listingname'><p />
Location:<br />
<input type='text' name='Location'><p />
Number of Beds:<br />
<input type='test' name='nobed'><p />
Zip:<br />
<input type='text' name='zip'><p />
Price:<br />
<input type='text' name='price'><p />
<label for="file">Filename(File must be exceed 4mb):</label>
<input type="file" name="file" id="file"><br>
<label for="file">Filename(File must be exceed 4mb):</label>
<input type="file" name="file1" id="file1"><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
}
?>
If i am talking about a better User Experience then Looping is much better then repeat upload and for repeat upload you can/should use ajax upload.
I'm a beginner in php and now doing a project in php. I want to upload images(maximum four image files only).I used the following code to upload images.
<script type="text/javascript">
count=1;
function add_file_field()
{
if(count<4)
{
var container=document.getElementById('file_container');
var file_field=document.createElement('input');
file_field.name='images[]';
file_field.type='file';
container.appendChild(file_field);
var br_field=document.createElement('br');
container.appendChild(br_field);
count++;
}
}
</script>
<div id="file_container">
<input name="images[]" type="file" id="file[]" />
<br />
</div>
<br>Add
I used the following code for single file upload.It's working
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "File Error : " . $_FILES["file"]["error"] . "<br />";
}else {
echo "Upload File Name: " . $_FILES["file"]["name"] . "<br />";
echo "File Type: " . $_FILES["file"]["type"] . "<br />";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "File Description:: ".$_POST['description']."<br />";
if (file_exists("images/".$_FILES["file"]["name"]))
{
echo "<b>".$_FILES["file"]["name"] . " already exists. </b>";
}else
{
$tmpname=$_FILES["file"]["tmp_name"];
$name=$_FILES["file"]["name"];
$new="jun.jpg";
rename($name,$new);
move_uploaded_file($_FILES["file"]["tmp_name"],"images/".$new);
echo "Stored in: " . "images/" .$new."<br />";
?>
Uploaded File:<br>
<img src="images/<?php echo $new; ?>" alt="Image path Invalid" >
<?php
}
}
}else
{
echo "Invalid file detail ::<br> file type ::".$_FILES["file"]["type"]." , file size::: ".$_FILES["file"]["size"];
}
?>
I need help to modify this code to upload maximum of 4 images.
Can rename function be used to rename a selected file for upload on moving to a specified folder?
but it was showing error
Please do help me
You should allow multiple file selection in your file input, so you do not have to add a new input over and over again:
<input id="file" type="file" name="images[]" multiple>
After submitting the form you can iterate over $_FILES array like that:
foreach($_FILES['images'] as $file) {
//your code here --> replace $_FILES['file'] with $file
}
I hope this helps.
<?php
if(isset($_POST['submit']))
{
$count=count($_FILES["images"]["name"]);
for($i=0;$i<$count;$i++)
{
if ((($_FILES["images"]["type"][$i] == "image/gif")
|| ($_FILES["images"]["type"][$i] == "image/jpeg")
|| ($_FILES["images"]["type"][$i] == "image/pjpeg"))
&& ($_FILES["images"]["size"][$i] < 100000))
{
if ($_FILES["images"]["error"][$i] > 0)
{
echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />";
}
else
{
echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />";
echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />";
echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("public/images/".$_FILES["images"]["name"][$i] ))
{
echo "<b>".$_FILES["images"]["name"][$i] . " already exists. </b>";
}
else
{
move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/". $_FILES["images"]["name"][$i] );
echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />";
?>
Uploaded File:<br>
<img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path Invalid" >
<?php
}
}
}else
{
echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." , file size::: ".$_FILES["images"]["size"][$i] ;
}
}
}?>
image uploaded using for loop..foreach was showing error
Here is an example of multi-file upload in PHP
https://github.com/hemantrai88/html5-php_multi-file-upload
I have one example which is working, I think this will help you
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" />
<input type="submit"/>
</form>
In php
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";
$desired_dir="user_data";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
The following form is on my page html page
<form action="upload_file.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>
This is the script for the php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000))
{
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"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]);
echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
When I press select on the form, nothing. It was working fine. The page with the form is called upload pictuture.html and the scriptt is called upload_file.php they are both on the root and the folder pics/2012/blackhall primary does exist and the pics dir is on the root also.
This was working but is not, can anyone see any errors I've made.
Thanks
Ross
Change your privileges and by whom script is executed (usually www-data or apache2) - this account must has write and probably read access to directory, where pictures are uploaded.
Anyway, checking file type based on $_FILES["file"]["type"] isn't safe. This can be easily falsified by hacker.
How do I set the form restrictions in php to only allow jpgs and pdfs for upload? Also, I seem to have an upload error when I set the file size to 800000. Here is the upload form:
<form action="upload_files.php" method="post"
enctype="multipart/form-data">
<label for="img_preview">Preview Image:</label>
<input type="file" name="img_preview" id="img_preview" />
<br />
<label for="pdf_doc">Your PDF:</label>
<input type="file" name="pdf_doc" id="pdf_doc" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
Here is the upload_files.php
<?php
if ((($_FILES["img_preview"]["type"] == "image/gif")
|| ($_FILES["img_preview"]["type"] == "image/jpeg")
|| ($_FILES["img_preview"]["type"] == "image/pjpeg"))
&& ($_FILES["img_preview"]["size"] < 80000))
{
if ($_FILES["img_preview"]["error"] > 0)
{
echo "Return Code: " . $_FILES["img_preview"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["img_preview"]["name"] . "<br />";
echo "Type: " . $_FILES["img_preview"]["type"] . "<br />";
echo "Size: " . ($_FILES["img_preview"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["img_preview"]["tmp_name"] . "<br />";
//check the file into its room
if (file_exists("upload/" . $_FILES["img_preview"]["name"]))
{
echo $_FILES["img_preview"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["img_preview"]["tmp_name"],
"upload/" . $_FILES["img_preview"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["img_preview"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
This is the upload code for the img_preview file. I would create another one for the pdf_doc file, but this one is not working right at the moment.
So, how do I increase the filesize limit to 8MB? And, for the pdf upload I would like to restrict the file type to pdf, jpg, or gif. So, I tried this as the parameters:
if ((($_FILES["img_preview"]["type"] == "image/gif")
|| ($_FILES["img_preview"]["type"] == "image/jpeg")
|| ($_FILES["img_preview"]["type"] == "image/pjpeg"))
|| ($_FILES["img_preview"]["type"] == "application/pdf"))
&& ($_FILES["img_preview"]["size"] < 80000))
but again errors. What's the fix? Is there a more fluid way to do this?
File type problem
Since the MIME type reported by the browser (which is what you get in $_FILES) can be spoofed, you can't rely on it. This leaves you with two reasonable options:
Filter by file extension
Filter using finfo_file
Unfortunately finfo_file is not included by default on PHP < 5.3, so this leaves you with the extension check (which has worked just fine for me in the past), or using the discontinued PECL package finfo.
Max upload size problem
There are many PHP settings that influence this, and you need to tweak all of them to have big uploads work. Fortunately, for some time now the PHP docs have a page where everything related to file upload that can cause problems is discussed.