Trying to get images to upload to a folder - php

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

Related

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

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.

php uploading file?

I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck

Uploading in 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.

file uploaded via php: no such file or directory

i'm working on a website wherein the users can upload images (uses php 4.3.11). the files are uploaded with no problem as i can see them in the upload directory and i don't get any error message, but when i try to access the uploaded files via ftp, i get an error: no such file or directory. sometimes, i am able to access the file sometimes i get this error. what could be the problem here?
[update]
thanks for the help guys. i'm not familiar with the ftp daemon stuff. but i do access my files via ftp using FireFTP. the files are there but when try to download them or change the file properties, i get the said error. i also tried uploading a file in the folder through ftp and i was able to download it with no problem.
here is some of the code i'm working on, its kind of roundabout but i'll see on how to improve it.
my working directory is something like this www.domain.com/register/
and the upload directory is here www.domain.com/register/uploads/
users are required to register and upon sign-up, a folder is created for them in the uploads directory. i couldn't find a way to create a folder without having to be in the uploads folder itself so i redirect to a create-user-folder.php file in the uploads dir.
the file just contained this code:
$user_foldername = rawurldecode($_GET['name']);
mkdir($user_foldername);
header("Location: ../form.php"); // redirect back to the page
i checked and the created folder's permission is set to 775.
and here's part of the code i use in uploading ( /register/function/function.php ):
$path = "../uploads/$user_foldername/";
for($j = 0; $j < $num_of_uploads; $j++){
if(is_uploaded_file($_FILES[$file]['tmp_name'][$j])){
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
}
}
i checked using FireFTP and the files are in the /uploads/user_foldername/ directory and its permission is set to 664. the strange thing is that when i try to download the files, at times there would be no problem at all but there are times when the error will appear.
[another update]
i added chmod() after the copy() function,
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
chmod($path.$filename, 0755);
but i still get the error.
another thing is that when i access /register/uploads/user_foldername/ through the url, i can see all of the uploaded files and view them, but how is it that i can't access them via ftp?
thanks again!
This is either a permission issue, or a configuration error. Here are things you should try:
What are the permission of the uploaded files? Does the FTP user has access to these files? Have you tried logging in as the user the FTP daemon would use and see if you could read the file that way?
Do you really see the correct directory? Have you verified by putting a file in that directory yourself and downloading it? Have you used the ftp command ls to verify the presence of the folder/folders/files?
You might need to chmod the folder the files are in, or in some cases the files themselves.
try chmoding them to 775
You can chmod files and folders through PHP it's self, with the chmod function. Or, you could use a FTP program such as filezilla.
Also check to make sure the intermediate directories are also permissioned as 755, as all the directories in the path need to be executable to be traversed.
i just figured out the problem. it was all because of the file name having accented characters in it, which explains why i do not always get the error message :|
<sigh> i should have seen this earlier, but anyway i hope this helps in case someone ran into the same problem.
thanks again! i really appreciate it :)

Categories