Question About move_uploaded_file() - php

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.

Related

Uploaded file is not transfering to the destination folder [duplicate]

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.

move_uploaded_file returns true but file does not appear in folder

I have a couple of scripts that worked fine on another server.
Now that I have moved everything on to a new server, the file does not appear in the destination folder. The strange thing is that move_uploaded_file returns true.
Also, I printed the post data and there is no error.
Can you guess what's possibly going on. The files I am trying to upload as a test are very small ( 10 kb).
The move likely succeeded, check the following:
You don't have access to view the file.
Use chmod("/path/to/file.ext", 0755); to add view rights for the ftp user.
You moved the file to an location that doesn't store the file. /dev/null
You're looking in the wrong folder. Did you use a full path?
The file is removed shortly after the move.
In my case, Total Commander, used as an FTP client, truncated output to 10000 files in directory. When I connected via SSH using WinSCP I was able to see all ~14000 files in the directory.

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

jQuery uploadify I/O error UNIX

I've a problem with jQuery uploadify script and I didn't found any solution.
I've integrated this script on my project and everything is working fine on a Windows server(localhost) but when I try to run it on an UNIX server and I/O error is risen.
This only happens when I try to upload a file that already exists on uploading folder. On Windows the file is overwritten but a UNIX I get and I/O error.
Please if you have any solutions I'll be very grateful.
Here is the server side code which I think is the problem(PHP code):
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'].$_REQUEST['folder'].'/';
$targetFile = str_replace('//', '/', $targetPath).$_FILES['Filedata']['name'];
if (file_exists($targetFile)) unlink($targetFile);
copy($tempFile, $targetFile);
echo "1";
}
First few things to check:
Which user owns the file that exists?
Which user owns the directory that the file is in?
Which user is running your PHP script?
What are the permissions on the file itself?
What are the permissions on the directory that the file is in?
The reason I ask these questions is because it may be a simple permissions problem. The user running the unlink and file create of the target file will need write access to the directory. It's possible that the user that's running the script is not the one that owns the directory or file, in which case you may have to open up the permissions a bit.
Of course, that's just conjecture on my part but that's the first thing I'd be looking for.
First of all, you should use move_uploaded_file instead of a copy.
But most likely your problem is due to a permissions problem. Can you upload any file to the folder? Have you checked that the files already in the folder (those you want to overwrite) have the same permissions as a newly uploaded one? My guess would be that you copied the files to the folder using (S)FTP and that they have a different owner/permissions so you can't overwrite them.
In addition to the possible permissions issues mentioned by paxdiablo and wimvds, also check the case of your filename & path. Unlike windows, unix filesystems are case-sensitive.

move_uploaded_file not working

I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.

Categories