move_uploaded_file not working - php

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.

Related

how to move uploaded file to a folder outside of the webroot?

Hi I want to create folders (with php) outside of the webroot and move uploaded files to them.
is that possible?how?
Use rename, copy or move_uploaded_file, though you need to make sure the folder has the correct permissions (write permissions for your webserver / php executing user).
Also, Handling file uploads might have some useful information for you.
Use php's move_uploaded_file() function.
Linked in a comment, however it bears repeating: Read the documentation.
Check if the directory exists and if not, create it:
if (!is_dir('/dir/path')) {
mkdir('/dir/path');
}
Move your uploaded file to the directory:
move_uploaded_file($_FILES["file"]["tmp_name"], "/dir/path");

php turning /tmp/ file to a $_FILE

I have JS uploading to PHP which creates a /tmp/ file. Then JS lets the user to edit the meta data to send back to the API.
The JS then sends back to my php API:
"image":"#/tmp/test.jpg"
From that, I would like to invoke a $_FILES['image'] object to copy and move_uploaded_file with
Is this possible? Thanks!
Nope, that's impossible. The very purpose of move_uploaded_file() function is to check if the file were really uploaded, not faked by some API.
You can use copy() instead but make sure you took all the precautions.
For uploaded files, the manual states:
The file will be deleted from the temporary directory
at the end of the request if it has not been moved away or renamed.
So you can move_uploaded_file to your custom temporary directory and respond to your client-side application with a new file name. Then you will probably need to write a script which cleans your temporary directory at some time (using cron)

Where does PHP save temporary files during uploading?

I am using XAMPP on Windows. By printing $_FILES["file"]["tmp_name"], it seems that the temporary file was saved at C:\xampp\tmp\phpABCD.tmp. But I cannot see it on the filesystem of the server. However, the file can be moved or copied via move_uploaded_file(), rename(), or copy(). So where does PHP actually save temporary files during uploading?
It saves it at the path specified in $_FILES["file"]["tmp_name"], but deletes it after the script is done executing. It's up to you to move the file elsewhere if you want to preserve it.
Its specified in upload_tmp_dir in your php.ini file. It is deleted by the system automatically after use.
You can check where php is currently saving your temp files $_FILES["file"]["tmp_name"] by printing
sys_get_temp_dir()
Use move_uploaded_file(file, path), specify the file and the path where you want to store the file.
A copy of that file is created and gets stored.
php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php.ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file). Another reason might be that the file is simply hidden on the file system.
So if you want to see the file, you might want to make sure you see all hidden files in the explorer and delay the script as long as you need to find the file.
from http://www.php.net/manual/en/features.file-upload.php#93602, "...the uploaded file will inherit the permissions of the directory specified in the directive upload_tmp_dir of php.ini. If this directive isn't set, the default of C:\Windows\Temp is used..."
Note that the file is saved binary in $_FILES["file"]["tmp_name"], so you may open it maybe with file_get_contents if it is an image or something like this...
IF you are asking the file location then it depend on the setting of server. but if you are asking whether it saved first in local system or in server. then answer is it save in temp folder in server.

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

'pass through' php upload to amazon's s3?

I've seen ways to upload files directly to S3 and I've seen ways to upload a file already on the server. I need to modify the file's name before sending it on to S3, but I don't want to store it locally and then upload it, if I can help it. Is there a way to put it in a buffer or something? Thx.
The file will always end up in the temporary directory first while the upload completes, even before you're able to work with the uploaded file. You get all the file's chunks before, and then it get rebuilt in the /tmp directory by default. So no, there's no "pass-through". But I guess you could re-upload directly from the temporary directory afterwards instead of moving it to another working directory.

Categories