bool(false) Uploading a file - php

I have had problems with a simple php script in which I can upload a file to a certain folder. I have tried multiple ways in doing this and I still have not had success.
Any errors in my code or advice on how to correct the issue will be taken gracefully.
Main Php Code:
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
Choose Photo:
<input name="userfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
<?PHP
if ($userfile_size>250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($userfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
</form>
</label>
</form>
Php code that is called upon on click (upload_photos.php)
<?php
$target_path="uploads/";
chmod("uploads/", 0755);
$target_path=$target_path . basename( $_FILES['uploadedfile']['name']);
$test=move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
if($test) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
var_dump($test);
}
?>
I do not understand why my end results [upon clicking "Upload Files" Button] include only the following results:
"There was an error uploading the file, please try again!bool(false)"
One more thing: I have also tried using the full computer folder path for $target_path and chmod.
Does anybody see what I am doing wrong?

You have <input name="userfile" but then use $_FILES['uploadedfile'] in your script - use one or the other.
Other than that, make sure the chmod worked and the folder is writable.

bool(false) is the output of var_dump($test);, indicating that move_uploaded_file is returning false.
As a basic debugging step, you should try var_dump($_FILES) to make sure you're accessing the right element of that array (I can tell from your code that you aren't, the index will be the name attribute of your <input type="file"/> element).
You have at least one other serious flaw in your logic... The PHP code in your upload form doesn't make any sense. That block of PHP code will execute server-side before the user has ever uploaded a file. It can't possibly work. The two variables you're checking, $userfile_size and $userfile_type, are not defined anywhere.

In my case, I forgot to create a folder where I want to upload. So check once the specified upload path is available or not.

Related

Uploading files with PHP & HTML

I'm having trouble with setting up the file upload to my php script.
Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right.
EDIT: I have resolved my situation with the file not properly uploading. I have edited the blocks of code to what I have currently working. If needed, please refer to the pre-edited version to see changes :).
Cheers.
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;
if(isset($_FILES["uploadFile"])){
$tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){
echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
The $targetnameis set to whatever the user inputs in the field. In this case, I have it as their email.
$targetname = filter_input(INPUT_POST, 'email');
This is what the <html> for the file upload looks like:
<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
<p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
<input type="submit" value="Upload File">
It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here?
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
This would be like:
C:/xampp/htdocs/uploaddir/me#gmail.com
This runs:
$tardir= $tardir . basename($_FILES["uploadFile"]["name"]);
then:
C:/xampp/htdocs/uploaddir/me#gmail.comfilename
shouldn't it be:
C:/xampp/htdocs/uploaddir/me#gmail.com/filename
you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. If I get a bug like this I dump everything and echo every string until I find it..

How to Upload a File within HTML Context

I'm trying to get a basic PHP script up and running where a user uploads a file to the server, and the file is saved to a folder on the server. I am using a HTML form that looks like
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50000000" />
<br />
<input type="submit" value="Upload" />
</form>
and I have a PHP file saved as upload_file.php that looks like
<?php
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) . "
has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The issue is that after the file is uploaded, it simply shows the php code. Nothing else occurs, and no image is saved in uploads. I've looked at several tutorials, and none of them seem to mention this. I'm guessing it's glaringly obvious, but I could use some help. The error echo "Sorry... never occurs either.
Thanks.
Quick Note: I'm using Apache to host a local web server.
If it simply shows the php code, then PHP is not running. Verify tha PHP is installed and mod_php5 activated.
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml

LAMP - file doesn't appear under tmp directory

I am uploading an image using an HTML form.
When I get the full path to the image, it outputs something like this:
'/tmp/phpkIv1BY/10259944_770025219687938_1184503840380306483_n.jpg'
but when I go to the /tmp folder, the sub-folder phpkIv1BY doesn't even exist! What's going on here?
Reason for this behaviour
All uploaded files are temporarily stored in the folder location as defined in
(In php.ini)
upload_tmp_dir =
(Read more about this: http://php.net/upload-tmp-dir)
These temporarily stored files are no longer exist after that php script execution(As mentioned in previous answer request life span) finished.
Do the following to view uploaded file while script executing.
upload.html
<html>
<body>
<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>
</body>
</html>
upload_file.php
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
$fp=fopen("/tmp/write.log","w+");
fputs($fp,"Original Name:".$_FILES["file"]["name"].";temporay Name:".$_FILES["file"]["tmp_name"]."\n");
fclose($fp);
sleep(50000);
}
?>
Upload file by upload.html
In another terminal apply tail in /tmp/write.log after uploading image(Because sleep time is too much so you able to find the image)
#tail -f /tmp/write.log
Copy that temporary file into any place and place the extension of original image file
For example my printed log line is
Original Name:digits.png;temporay Name:/tmp/phpOKlpoK
(Needed to do this with root privilages)
#cp /tmp/phpOKlpoK /home/sandeep/Desktop/file.png
#chown sandeep.sandeep /home/sandeep/Desktop/file.png
So this is the uploaded file that you.
(
Instead of doing all these steps for checking uploaded images you can use move_uploaded_file()
)
I can not find anywhere if PHP might create a temporary folder for temporary uploaded files. But what is certain is that uploaded files are kept temporary and as soon as the request is done, they are removed. So if you think you can find some uploaded file in the /tmp folder, think again.
If you would like some uploaded file to live longer than a request life span, then you need to move it somewhere safe using move_uploaded_file().

Uploading images with size limitations in PHP/MySQL

I'm trying to figure out how to upload imaged to a folder. I don't want to list it by listing all files in the directory.
What I want to do is have the user insert certain values to put into the MySQL, then take the file name when the file is finished uploading and insert it into a MySQL field "filename".
Thing is, I don't really know how to do file uploads in PHP, much less how to have it not add the information to the database until the file is fully uploaded. Could someone please help me out?
I also wanted to know how I could limit the max filesize to a certain size. I haven't been following technology for a while, so I wanted to know what the average size of an image would be of a photo take with an average digital camera now of days?
Lets make sure we have a form in a page. Lets call it form.php:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Then we create a file called uploader.php in the same folder as form.php.
// change the username and password to your database information
mysql_connect("localhost", "username", "password");
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
// Here we will insert into the database
mysql_query("INSERT INTO fileuploads (filename) VALUES ('".$target_path."')");
Now make sure that you have a table called fileuploads with the field "filename".
Don't forget to create a directory in the same folder as form.php and uploader.php called "uploads"
I hope this will work for you.
Well you seem to have broken it down into the steps yourself so you seem to understand what to do.
The first thing you need is to upload the file in PHP. You can find a tutorial on it here. You will need a form in HTML and a PHP file which will handle it. This is also where you specify the file size.
Sample HTML
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Sample PHP
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Then once you have uploaded the file you will need to store it in your database. You need to connect to your database (tutorial here)
Then you will need to add the information to the table, the filename is in the var $_FILES['uploadedfile']['name'] so you would need some code along the lines of
mysql_query("INSERT INTO files (Date, Filename)
VALUES ('$date', '$_FILES['uploadedfile']['name']')");
More information on inserting into a database can be found here
You can easily find out the PHP file upload details with example from this url http://php.net/manual/en/function.move-uploaded-file.php
And to verify the size, you should use getimagesize() function in php. Check the example code here http://php.net/manual/en/function.getimagesize.php

Using only PHP and HTML to act like JavaScript

I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.
We need to get an uploaded file as a temp file only (index.php)
Output size of file (upload.php)
User can confirm save of file or not (upload.php)
So, I have the majority down, but my problem lies with creating the temp file into a permanent file.
index.php
<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php
<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
}
?>
<form action="" method="POST">
<input type="submit" value"YES please save">
<form>
<?php
if (isset($_POST['submit']))
{
//Code for saving file
echo 'File saved!';
}
?>
Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.
Hopefully the following comments can help you with the part you are stuck on.
In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.
One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.
Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.
I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.
Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.
Hope that helps.
Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:
<input type="submit" value"YES please save">
Should be
<input type="submit" value="YES please save" name="submit">
Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.
The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.
This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.

Categories