1

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
hanzichi
  • 609
  • 2
  • 12
  • 22

4 Answers4

2

You can increase that in php.ini

upload_max_filesize = 180M
max_file_uploads = 180
upload_max_filesize = 180M
Mansoor H
  • 594
  • 1
  • 8
  • 25
1

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)

Community
  • 1
  • 1
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
0

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 :)

Domuta Marcel
  • 509
  • 5
  • 16
-1

Try :

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

This works perfectly !

Look at : PHP File upload

SatanicGeek
  • 342
  • 5
  • 15