Uploading in php - php

I'm going through the O'Reilly book Learning PHP & MySQL 2nd ed. by Michele Davis & Jon Phillips.
I'm stuck on example 11-28. The goal is to upload a picture and move it from /tmp to an uploads folder if it meets certain conditions: file size, type, and whether or not it was uploaded. This is being stored on my home Ubuntu server.
The form is here, and the code is here. Having a hard time getting it to show up.
When I hit the submit button w/o there being a file in the file box, I get the following warning:
Warning: unlink() [function.unlink]: No such file or directory in /home/luna/public_html/learn_php/up_urs.php on line 10
When I submit a file bigger than maxsize I get my error message:
Error. File must be less than 28480 bytes.
When I submit a file of the wrong type I get my error message:
You may only upload .gif & .jpg files.
When I submit a file that's the right type & size I get the following warning and my OK message:
Warning: move_uploaded_file(learn_php/uploads/) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/luna/public_html/learn_php/up_urs.php on line 21
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php4Mhykl' to 'learn_php/uploads/' in /home/luna/public_html/learn_php/up_urs.php on line 21
Thanks for your upload.
What I've tried:
I thought the problem was permissions but changing the directory learn_php to 777 made no difference so I changed it back to 755.
Changing the path from "uploads" to /home/luna/public_html/learn_php/uploads
Using getcwd() before & after the unlink & move_uploaded_file commands but that only told me I was in learn_php...
I tried uncommenting //print_r($_FILES); but that didn't show me anything after pressing submit.
Not sure what to look for now. Thanks for any help :)

The first error you see is because you are trying to unlink a file that doesn't exist on line 10. After you do the is_uploaded_file() check you should do a quick if ( isset($_FILES['upload_file']['tmp_name']) ) before doing the unlink.
The last two warnings are because it seems you don't have the upload directory already created. I don't think move_uploaded_file() will create it for you. So just create that directory and make sure that it has proper permissions to be written to.
As a beginner, I would also recommend checking the return values of these functions so that you don't return 'all good' when things have actually failed for some reason. Start putting in code like if ( !move_uploaded_file($_FILES['upload_file']['tmp_name'], "/path/to/learn_php/uploads/".$_files['upload_file']['name']) ) { $error = "Could not move the file to the uploads directory."; } else { print "All good, thanks for the upload."; exit(); }

See my answer here. That will give you a function to find the current root directory, which you can use as a prefix for file uploads and image manipulation.
Then, before running unlink, do an is_file() check first (and before moving it, you could run it to to ensure it exists in tmp if you are paranoid)

Rather than guessing your code path try working down from the top.
$uploadpath=$_SERVER['DOCUMENT_ROOT'].'\learn_php\uploads\';
I might be wrong, since you didnt post your source, but you are missing a slash in front of a directory, so it may be trying to store it in \learn_php\learn_php\uploads\ which wont exist, since the PHP file is already in that directory, so it is attempting to access a sub directory.
if all else fails, you will probably want to brush up on the docs.
http://php.net/manual/en/features.file-upload.php

Warning: move_uploaded_file()
[function.move-uploaded-file]: Unable
to move '/tmp/php4Mhykl' to
'learn_php/uploads/' in
/home/luna/public_html/learn_php/up_urs.php
on line 21
This is telling you that the folder you are trying to move to (learn_php/uploads/) doesn't exist. Double check that you've created that folder on your server.

Related

TCPDF unable to output file by saving. Path on Mac?

I checked some similar questions here, but i didn't got an answer, solving my problem.
I use TCPDF to generate a PDF with PHP.
When I use the $pdf->Output($file_total, 'I');
it's all good and the file is shown in Browser.
If I use the save to localhost option $pdf->Output($file_total, 'F'); I get this error:
"failed to open stream: Permission denied in /opt/lampp/htdocs/project/tcpdf_min/include/tcpdf_static.php on line 1821"
"TCPDF ERROR: Unable to create output file"
The Path I set for saving the PDF file is: "/opt/lampp/htdocs/project/files/2021"
What am I doing wrong? is my path wrong?
The forced saving $pdf->Output($file_total, 'D'); works just fine, so i must doing something wrong with the path on mac...
please help!
Thanks
In comments we both went through possible reasons such as finding an absolute rather than relative location and checking out folder permissions for non admin process as that was also suspected. Thus between us the conclusion was:-
use address as
"localhost:8080/projects..."
and ensure target folder was set to permissions
"read/write" for all users
For production use the write access needs to be trimmed back to the process and admin, not all users.

php move_uploaded_file wont move the file to the hosted server

I don't know how to fix this but the problem is that it won't upload to the server that I'm using to host the website it creates the folder just fine but won't move it. The coding for the moving is below.
This is the error I get
Warning: move_uploaded_file(./userdata/profile_pics/iOy1pQXTZsLw7VA/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/a4640336/public_html/account_settings.php on line 103
and as well as this error code
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpX1zVno' to './userdata/profile_pics/iOy1pQXTZsLw7VA/' in /home/a4640336/public_html/account_settings.php on line 103
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"./userdata/profile_pics/$rand_dir_name/".$FILES["profilepic"]["name"]);
echo "Your profile pic has been updated!".#$_FILES ["profilepic"]["name"];
//$profile_pic_name = #$_FILES["profilepic"] ["name"];
//$profile_pic_query= mysql_query("UPDATE users SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$username'");
//header("location: account_settings.php");
Overall I have tried to change where it is located to have it leading directly from the source but it doesn't change. If anyone can help please help me!
PS the commented out parts were done to be able to see the error
For those using PHP on Windows and IIS, you SHOULD set the "upload_tmp_dir" value in php.ini to some directory around where your websites directory is, create that directory, and then set the same permissions on it that you have set for your websites directory. Otherwise, when you upload a file and it goes into C:\WINDOWS\Temp, then you move it to your website directory, its permissions will NOT be set correctly.And If you try to upload a file larger than the post_max_size value (or multi files), the page will only refresh itself and no errors are thrown.
The destination directory must exist; move_uploaded_file() will not automatically create it for you.
You must
make sure that the file is not empty.
make sure the file name in English characters, numbers and (_-.) symbols, For more protection.
make sure that the file name not bigger than 250 characters.
Check File extensions and Mime Types that you want to allow in your
project. You can use : pathinfo(). or you can use regular expression for check File extensions as in example
Check file size and make sure the limit of php.ini to upload files
is what you want, You can start from here.
Check the file content if have a bad codes or something like this
function
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);
Also check dir have writable permission
you need to use server path for file upload
$files = glob($_SERVER["DOCUMENT_ROOT"]."/myFolder/*");
$_SERVER["DOCUMENT_ROOT"] will get server path like var/host/public_html/your_folder
May this help you

Trying to get images to upload to a folder

Hi I have an upload script that uploads images to a file, but I can not get to save in a file that isnt in the same folder as the php script.
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move
'/tmp/phpkiIfQ' to 'http://--Webaddress--/Logoimages/bug4.png' in
/home/--host--/public_html/uploaded/upload3.php on line 48
Removed some details and replaces with "--relevant--"
Here is the code I have on line 48
"http://--WebAddress--/Logoimages/" . $_FILES["file"]["name"]);
Any help would be great thanks,
Mikey
Thanks you to everyone who answered
Are you actually using a HTTP URL as the location to move files to? That's probably where you're going wrong. Try using a local file path. If you need to go up and down the tree the pseudo directory .. will be useful.
change to
"../Logoimages/" . $_FILES["file"]["name"]);
You can't move the file to a url - you need to move it to the location on disk which maps to that Url - eg /srv/www/htdocs/Logoimages/somefile.png (The exact path will depend on what flavour of linux you're running and how your sites are set up)
Also, make sure your website user has permissions to write to that folder

PHP move_uploaded_file() FAILS and i don't know why

this is my code:
$uploaddir = '/temp/';
$uploadfile = $uploaddir.basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
send_OK();
else
send_error("ERROR - uploading file");
i have tried to upload with ftp_fput, ftp_put, move_uploaded_file, rename, copy and anything i can put my hands on. nothing seems to work.
i can't understand what is the problem since move_uploaded_file returns only true or false and no error code.
help??
Are you sure that the target directory has write permissions for world?ie,the third number in permission representation?
The files uploaded by php are owned by and comes under the group www-data
You can change the ownership by
[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner
i don't know why
But you have to.
That's what error messages are for.
Do you see any error message when something goes wrong? If not, then you have to check error logs.
Add this line at the top of your code
error_reporting(E_ALL);
and this one, if it's your local (not live) server
ini_set('display_errors',1);
so you'll be able to see errors onscreen
For the file uploads you have to check $_FILES['file']['error']) first. it it's not 0, refer to the manual page for the actual message.
I experienced a similar problem when using move_uploaded_file which would fail to upload particular files with an $_FILES['filename']['error'] code of 0.
It turns out that the name of the file needs to be unique in relation to the destination directory. move_uploaded_file does not know how to handle identical files names.
Have you check the limit of the file size? One of the reason if crashing could be that you are trying to upload a file bigger than the limit in your configuration. Look at the config var "upload_max_filesize" in your php.ini and check the size of the file.
This caught me out too. Be aware of:
move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the destination path as to allow the moving of uploaded files in which filename may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.
These settings can cause the upload to fail if you try to move the file outside of your website base directory for example.
In addition to permissions, be sure to check that there is disk space available on your server. If not, move_uploaded_file() will fail with error 0.
Did you try to activate error_reporting?
You should check your php-config if file uploads are allowed.

Question About move_uploaded_file()

I used to have a php file that does a simple move_uploaded_file by using selecting a local file and upload to our UNIX web server.
Now we migrate our code to a Windows2003 Server, then the move_uploaded_file() fails, the error that keeps coming up reads like:
"Cannot move the C:Windows\temp\100D.php" file to desiredDirectory.
here desiredDirectory means it caputures the correct directory for this file movement. The code we used is pretty straightforward:
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
and we did try change it to $HTTP_POST_FILES, but still not working.
So we are really clueless at the moment, wonder if any experts could give us some hints, thanks a lot.
You should check if the target directory exists and if the apache user has all rights on that folder.
For a test you can set the folder access settings for the user 'everyone' to 'full'
The snippet of your code i see here is correct and you don't have to use $HTTP_POST_FILES
Does the webserver have write permissions on the target directory? Given that you say the paths are correct, that's the other #1 major reason why file moves fail.

Categories