i have a general question about php, i can't understand what does $ _FILES ["upload"] ["tmp_name"], why should i upload a file into a tmp folder, and not direclty into a permanent folder? Thanks for read, have a nice day!
1 Answer
The PHP interpreter puts an uploaded file in the temporary directory using a generated name and stores the path in $_FILES['...']['tmp_name'] before running your PHP script.
You can use is_uploaded_file() to make sure the content of $_FILES['...']['tmp_name'] is indeed the path of an uploaded file (and it was not spoofed somehow in the request) then use move_uploaded_file() to put the file on its final destinations.
Or you can also process the content of the file without moving it, in case you don't need to store the file.
Either way, when your script ends the interpreter removes the temporary files it created to store the uploaded content.
The code usually looks like:
if (is_uploaded_file($_FILES['abc']['tmp_name'])) {
// Generate the path where to store the file
// Depending on the expected file type you can use getimagesize()
// or mime_content_type() to find the correct file extension
// and various ways to generate an unique file name (to not overwrite
// the file already existing in the storage directory)
$finalpath = '...';
if (move_uploaded_file($_FILES['abc']['tmp_name'], $finalpath)) {
// Successfully uploaded and processed.
} else {
// Cannot move the file; maybe there is a permissions issue
// or the destination directory simply doesn't exist.
}
} else {
// The upload failed.
// Check the value of $_FILES['abc']['error'] to find out why
// (usually no file was uploaded or the file is too large).
}
Read about the PHP way to handle file uploads.
2 Comments
is_uploaded_file() with move_uploade_file() as it performs the same check before move operation. is_uploaded_file() is useful if you need to perform some other action.
$_FILES['...']['tmp_name']before running your PHP script.