Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move_uploaded_file() function is not working

Tags:

file

php

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> 
like image 233
user2480054 Avatar asked Sep 21 '13 05:09

user2480054


People also ask

What is use of move_uploaded_file in PHP?

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.

How to get upload file path in PHP?

header("Content-Length: ". filesize($filePath)); Where $filePath should be absolute path to file not just file handle.

How we save upload file in folder in PHP?

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

How do I move a file into permanent directory?

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.


2 Answers

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"; } 
like image 109
Vlad Miller Avatar answered Sep 19 '22 07:09

Vlad Miller


Try using copy() function instead of move_uploaded_file(). It worked for me.

copy($_FILES['file']['tmp_name'], $path); 
like image 32
user3502785 Avatar answered Sep 20 '22 07:09

user3502785