I'm working with php/html5 and i'm attempting to upload a file, but $_FILES['picture'] never seems to contain anything. I've been through a lot of posts and looked for common fixes, but none of them seem to work, firstly, the code;
Form;
<form enctype="multipart/form-data" action="decodeQR.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<input type="file" name="picture" id="picture" value="picture" accept="picture/*" capture>
<input type="submit" value="Upload">
</form>
decodeQR.php;
<?php
include 'header.php';
$upload_status = FALSE;
if(isset($_FILES['picture']))
{
echo 'picture set <br>';
}
else
{
echo 'picture not set <br>';
}
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
if (isset($_FILES['picture']) && file_exists($_FILES['picture']['tmp_name']))
{
$image = $_FILES['picture']['tmp_name'];
//~ Check if image is an image
if (#getimagesize($image))
{
$upload_status = TRUE;
//~ from here you can use yours image as $_FILES['picture']['name'], for example to copy it
move_uploaded_file($image, realpath(dirname(__FILE__)).'/images/'.$_FILES['picture']['name']);
//~ Also be noticed that the image curently is in OS tmp folder and if you dont copy it, it will be deleted after script execution.
}
}
if ($upload_status)
{
echo 'Image successfully uploaded. <br> <img src="images/'.$_FILES['picture']['name'].'">';
}
else
{
echo 'nope.jpg';
}
?>
The output is always;
picture not set
nope.jpg
This means that $_Files['picture'] is not set, and there are no errors in the files array.
As you can see from the code above i have already tried the following fixes;
Added the markup for form enctype; enctype="multipart/form-data"
Added a hidden MAX_FILE_SIZE attribute
Not show in the code, i have tried adding size='30000000' in the file tag
I've checked that the value / name are the same when setting and getting file
I've also checked php.ini to ensure that file_upload is allowed
What could I possibly be missing?
Edit; I've tried this on my desktop and mobile browsers.
I found the solution to actually be a problem between the obvious (Not being able to file upload using ajax), and jquerymobile framework, which uses ajax on it's forms by default.
To fix the problem I added data-ajax='false'
<form enctype="multipart/form-data" action="decodeQR.php" method="post" data-ajax='false'>
The file upload works fine, so i'm posting this answer for anyone who's using jquerymobile and comes across this problem! : )
Have you checked if the request sent by the browser contains the file?
BTW. I'm new here. How do you guys add these "comments" to questions?
Related
I am trying to upload a local file to webserver using HTML POST method and PHP.
this is my php code:
<?php
if (isset($_POST["submit"])) {
$updir = "/var/tmp/";
$upfile = $updir.basename($_FILES['rawexcel']['name']);
if(is_uploaded_file ($_FILES ["rawexcel"]["tmp_name"]))
{
move_uploaded_file ($_FILES["rawexcel"]["tmp_name"], $upfile);
} else {echo "error uploading file ".$upfile;}
} else {echo "not isset post method";}
?>
and HTML code is:
<div class="container" id="upl">
<h4> Upload files</h4>
<form action="upl.php" enctype="mutipart/form-data" method="post">
<p> upload your files to DB</p>
<p><input type="file" name="rawexcel" id ="rawexcel">
<input type ="submit" value="Upload" name ="submit"></p>
</form>
</div>
$_FILES["rawexcel"]["error"] shows 0 and from running this peice of code i get
error uploading file /var/tmp
I guess file name was not retrieved from html?
Error is in enctype:
enctype="multipart/form-data"
not:
enctype="mutipart/form-data"
You have typo mistake in enctype="multipart/form-data" , instead of this you typed enctype="mutipart/form-data" . So "mutipart" spelling is need to correct.
I am trying to build a web page where users can edit images portrayed on their public pages. There are 3 images that display on their public page and I've set up 3 HTML forms in order to handle the 3 separate files.
I only have 1 listed below because once I figure out the fix to 1 I'll be able to duplicate the fix to the other 2,
I have other upload pages on my website and they work fine, but for some reason this page is giving me trouble. I can select a file but when I want to upload it my php code doesn't read that there is anything being posted.
*I've commented out the function call (I know it works) I just need to know why my php code won't read that there is a file there.
If I had to guess it'd be something with how it's named or how it's being tossed to the php code.
The code looks like this:
<div class="academic" style="width:250px;">
<br>
<?php
if(isset($_FILES['aca']) === true)
{
echo 'please print'; //It doesn't
if(empty($_FILES['aca']['name']) === true)
{
echo 'Please choose a file! <br>';
}
else
{
$allowed = array('jpg', 'jpeg', 'gif', 'png');
$file_name = $_FILES['aca']['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['aca']['tmp_name'];
echo '<br>';
echo '<br>';
print_r($_FILES['aca']['tmp_name']);
echo '<br>';
echo '<br>';
if(in_array($file_extn, $allowed) === true) {
//upload_image($file_temp,$file_extn);
echo 'You have uploaded a picture!';
echo '<b><h3>Press submit again to finish the upload</h3></b>';
//echo "<script>window.top.location='../hidden/viewPNM.php?id=$permi'</script>";
}
else
{
echo 'Incorrect File Type!! <br> Allowed: ';
echo implode(', ', $allowed);
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="aca" id="aca">
<input type="submit" value="Upload">
</form>
</div>
</span>
Okay...here is what you can try to do:
Move the whole PHP code UNDER the form code. I remember that sometimes wierd bugs with that were happening, and the code didnt run
If that doesnt work, do following:
Change <input type="file" name="aca" id="aca"> to <input multiple="true" type="file" name="aca[]" />
Add this code above the submit button: <input type="hidden" name="sendfiles" value="Send Files" />
Replace if(isset($_FILES['aca']) === true) with if(isset($_POST['sendfiles']))
If this doesn't work, I can offer you a way to allow multiple files being uploaded from one button. (Took the code from my website I made before)
I had file upload working fine this morning and did a couple of test uploads using small csv files, but when I did another test it has stopped working.
var_dump of $_FILES['file'] is NULL, and trying if($_FILES) produces nothing.
No one else uses my server so I know nothing's been changed.
Here is my form which posts to the same page.
<form method="post" enctype="multipart/form-data" style="border:1px solid #999">
<input type="file">
<input type="hidden" name="customerID" value="<?=$_GET['customerID']?>">
<input type="submit" value="Import">
</form>
Php handler:
if($_FILES)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = "files/".$_POST['customerID']."/";
echo $path."<br>";
if(!file_exists($path)) mkdir($path);
$path = $path.basename($_FILES['file']['name']);
echo $path."<br>".$_FILES['file']['tmp_name']."<br>";
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) echo "Upload success";
else echo "Upload failed";
}
else echo "No temp file";
}
You had to change something... because $_FILES['file'] should be undefined in code you gave. No file is sent at all by most browsers in the case when file input has no name attribute in HTML form.
Simply change:
<input type="file">
to
<input type="file" name="file">
You need to add a "name" attribute to your <input type="file">. The name you give to the input will be the index of the array element in $_FILES that contains the uploaded file data.
Let me explain what i am hope to acomplish:
I want to allow my users to upload a image as avatar.
I found many php upload tutorials but i don't know hoy to upload the avatars as user_id.ext in /avatars folder.
I hope i was clear, thanks.
In any upload script, you go through a few basic steps. First, you get data from $_FILES telling you where the temporary upload file is. You validate the file based on something to make sure it's not evil/malicious/wrong. Then you rename it and move it somewhere useful. In your last step, when you move the image to where it's going, take that opportunity to name the file as you like. If you're dealing with a user's account it should be trivial to get the username, id, middle name, etc and use that to set the file's name.
This script gets the uploaded file and save it as /avatars/$user_id.ext, $user_id retrieved from POST:
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "/avatar/{$_POST['user_id']}.ext");
echo "Stored in: " . "/avatar/{$_POST['user_id']}.ext";
}
?>
And this is the form:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="hidden" name="user_id" value="<?php echo $user_id ?>">
<input type="submit" value="submit"></form>
I would like to have the user upload a pdf to a folder on my website. (note:this is for learning purposes, so security is not necessary) The code I have below does not do echo a response when submitted. The folder I would like to have the pdf uploaded to is in the same directory as the php script, is it possible I'm incorrectly referencing that folder? I appreciate it.
<form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
Email:<br /> <input type = "text" name="email" value=""/><br />
Resume:<br /><input type = "file" name="resume" value=""/><br />
<p><input type="submit" name ="submit" value="Submit Resume" /></p>
</form>
if(isset($_POST['submit']))
{
define ("FILEREPOSITORY","./resume/");
if (is_uploaded_file($_FILES['resume']['tmp_name'])) {
if ($_FILES['resume']['type'] != "application/pdf") {
echo "<p>Resume must be in PDF Format.</p>";
}
}else {
$name = $_POST['email'];
$result = move_uploaded_file($_FILES['resume']['tmp_name'], FILEREPOSITORY."/$name.pdf");
if ($result == 1) {
echo "<p>File successfully uploaded.</p>";
}
else {
echo "<p>There was a problem uploading the file.</p>";
}
}
}
You have a logical error. Your else statement should be part of the inner if statement -- not the outer one.
would suggest you check the permissions for the upload folder and the max size for file uploading in your php.ini... its happened to me many times uploading a file exceeding the limits and not getting an error message.. also the logic of your if else doesn't match as suggested by your previous post..
IT would be of great help to give the error you receive.
move_uploaded_file()
only works if you have the rights to write to the destination folder.