简体   繁体   中英

Why can't I get $_FILES[“file”][“tmp_name”] when uploading a zip file?

It's my first time here and I wonder if you could understand my question for I am not from an English-speaking country.

I want to write some code to upload a file, my code is quite easy to understand if you are know PHP or JS. Here is my code.

<form action='data.php' method='post' enctype='multipart/form-data'>
  <input type='file' name='file' />
  <input type='submit' />
</form>

$user_uid = rand(0, 50000);
$file_pos = strpos($_FILES['file']['type'], '/');
$file_name = $user_uid . '.' . substr($_FILES['file']['type'], $file_pos + 1);
$savePath = dirname(__FILE__) . '\\' . $file_name;
copy($_FILES['file']['tmp_name'], $savePath);

As above, I want to get the tmp_name of a file, but it does not work if a zip file or 7z file is uploaded. But it seems to work quite well for jpg or others, why?

I tried to output the $_FILES['file'] of a 7z, example below:

Object {name: "2.zip", type: "", tmp_name: "", error: 1, size: 0}

I find the tmp_name is "" so does its type if 7z, how does that happen? And if I want to upload a zip or 7z, how should I change my code?


@2017.09.13

increase that in php.ini and restart the server

upload_max_filesize = 180M
max_file_uploads = 180
upload_max_filesize = 180M

You can increase that in php.ini

upload_max_filesize = 180M
max_file_uploads = 180
upload_max_filesize = 180M

Reading http://php.net/manual/en/features.file-upload.errors.php shows error = 1 to be

UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

So increasing the upload_max_filesize limit would solve your issue (this will help Changing upload_max_filesize on PHP )

Try this mothod:

if(isset($_FILES["file"])){
$allowed =  array('rar','zip' ,'7z', 'RAR', 'ZIP', '7Z');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo '0';
} else {
// If no errors, upload the file
$target = "../fisiere_pub/"; //choose your upload folder
move_uploaded_file($_FILES["file"]["tmp_name"], $target. $_FILES["file"]["name"]);
$file = $target . $_FILES["file"]["name"];
 }
} else $file = "Upload error!";
echo $file;

If this code is not working, try with small files (up to 5mb) or change the maximum filesize for upload in php.ini as other users mentions :)

Try :

move_uploaded_file($_FILES['file']['tmp_name'],$NewPath.$FileName);

This works perfectly !

Look at : PHP File upload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM