I'm working on a website and I want the user to be able to upload files. So I'm trying to learn how to do that. I researched and it said that I had to use the function move_uploaded_file(). I wrote the code just like it was on the example (changing the data), but it wouldn't work. Please help me, I'm new at these. Here's what I've done so far:
<!DOCTYPE html> <html> <head> </head> <body> <form action="upload_file.php" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <input type="file"name="file"> <input type="submit"> </form> </body> <html>
This is the upload_file.php:
<!DOCTYPE html> <html> <head> <head> <body> <?php $move = "/Users/George/Desktop/uploads/"; echo $_FILES["file"]['name']."<br>"; echo $_FILES["file"]['tmp_name']."<br>"; echo $_FILES["file"]['size']."<br>"; echo $_FILES['file']['error']."<br>"; move_uploaded_file($_FILES['file']['name'], $move); ?> <body> <html>
Definition and Usage The move_uploaded_file() function moves an uploaded file to a new destination. Note: This function only works on files uploaded via PHP's HTTP POST upload mechanism. Note: If the destination file already exists, it will be overwritten.
header("Content-Length: ". filesize($filePath)); Where $filePath should be absolute path to file not just file handle.
php $target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path...
PHP stores uploaded files in a temporary directory with temporary file names. You must move uploaded files to a permanent directory, if you want to keep them permanently. PHP offers the move_uploaded_file() to help you moving uploaded files. The example script, processing_uploaded_files.
The file will be stored in a temporary location, so use tmp_name instead of name:
if (move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../../uploads/'. $_FILES["image"]['name'])) { echo "Uploaded"; } else { echo "File not uploaded"; }
Try using copy()
function instead of move_uploaded_file()
. It worked for me.
copy($_FILES['file']['tmp_name'], $path);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With