Upload folder files and save to different directories - php

I'm using this code to upload folder files.
<form action="file_transfer.php" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file_input[]" id="file_input" multiple="" webkitdirectory="">
<input type='submit' class='btn' id='btn_upload_file' value='UPLOAD' data-loading-text='loading....'/>
</form>
And file_transfer.php
$uploads_dir = 'C:/xampp/htdocs/awoc_inspection/Images/IMAGES/';
for($i = 0; $i < sizeof($_FILES['file_input']['name']); $i++){
$tmp_name = $_FILES['file_input']['tmp_name'][$i];
$name = $_FILES['file_input']['name'][$i];
echo $_FILES['file_input']['tmp_name'][$i];
echo $_FILES['file_input']['name'][$i];
echo"<br/>";
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
It does not show any error but when I go to C:/xampp/htdocs/awoc_inspection/Images/IMAGES/
folder there is no image. I don't know why this code can't move uploaded folder files please help me :(

It doesnt print error cause you didnt ask it to do:
echo $_FILES["file"]["error"][$i];
The problem might be that your directory doesnt have good permission try chmod 775 or 777
Also do:
echo $result = move_uploaded_file($tmp_name, "$uploads_dir/$name");
to check whether $result is true or false, if false then moving file wasnt done

Try to change the for condition with:
for($i = 0; $i < sizeof($_FILES['file_input']); $i++) {
...

I've overcome own my problem :) .. thank's everyone for your answers ... the only problem why I can't upload pictures it is because the picture that I'm trying to upload is 3mb above and the maximum upload file_size in php is 2mb .
I've changed php.ini to
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 50M

Related

PHP : Uploading multiple files [duplicate]

This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 4 years ago.
trying to upload multiple files
Move_uploaded_file not working.
file permission user#777
$file = $_FILES['photo'];
$count = count($file['name']);
for($a = 0 ; $a<$count ; $a++)
{
echo $_FILES['photo']['name'][$a];
$target_dir = preg_replace('/\s+/',
'',$name.'_'.basename($_FILES['photo']['name'][$a]));
$target_file = "../upload/".$target_dir;
echo $target_file;
if(move_uploaded_file($_FILES['photo']["name"][$a], $target_file))
{
echo 'yes';
}
else
{
echo 'no';
}
}
You maybe pointing to the wrong path. Make it complete by using $_SERVER['DOCUMENT_ROOT'].
This:
$target_file = "../upload/".$target_dir;
to:
$target_file = $_SERVER['DOCUMENT_ROOT']."/upload/".$target_dir;
The source file should be 'tmp_name' one, not the 'name'.
From:
if(move_uploaded_file($_FILES['photo']["name"][$a], $target_file)){
To:
if(move_uploaded_file($_FILES['photo']["tmp_name"][$a], $target_file)){
If the above changes still don't fix it:
Check your php.ini
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =
; Maximum allowed size for uploaded files.
upload_max_filesize = 50M
; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

Print/debug cause of upload error in PHP

I'm a PHP newbie and have written a script to process form uploads. It works for small files(less than 1Mb). However when I try to upload an ~4Mb pdf file it returns an error message. What am I doing wrong?
PS: I ran php_info, got the value of "upload_tmp_dir" and set it to a directory owned by the apache process(www-data)
My client-side code is
<form action="upper.php" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"><br>
<input type="submit" value="Upload">
</form>
Upper.php contains
define("UPLOAD_DIR", "/var/www/html/upload/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "Error is " . $myFile["error"];
//echo "<p>An error occurred.</p>";
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}
Most probably you have wrong config in you php.ini file.
You need to set
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
its already described here: PHP change the maximum upload file size
You ned to set values in php.in file.
Open the file,
Find upload_max_filesize and post_max_size lines.
Change this two value. (M= MB)
; Maximum allowed size for uploaded files.
upload_max_filesize = 20M
; Must be greater than or equal to upload_max_filesize
post_max_size = 20M
Restart the sever for effect.
Note:
You can set any number. But not use any decimal value.
Open the php.ini file in NOTEPAD. Don't user wordpad or any word processor.

unable to upload two files in php

I want to upload the files in html as follow :
<tr><td><?php _e("Upload Trust Logo","emarksheet"); ?></td><td><label class="btn btn-danger" for="file-sel"><input id="file-sel" type="file" name="imaget" style="display:none;" size="25" />Browse to Upload Logo ....</label></td></tr>
<tr><td><?php _e("Upload Institute Logo","emarksheet"); ?></td><td><label class="btn btn-primary" for="file-sel2"><input id="file-sel2" type="file" name="image" style="display:none;" size="25" />Browse to Upload Logo ....</label></td></tr>
Two upload these files. My php code is as follow :
$path = plugin_dir_path(__FILE__);
$file = $_FILES['image'];
//print_r($_FILES);
$name1 = $file['name'];
$type = $file['type'];
$size = $file['size'];
$tmppath = $file['tmp_name'];
move_uploaded_file ($tmppath, $path.'logos/'.$name1);
//upload data end
//upload trust logo`
$file2 = $_FILES['imaget'];
//print_r($_FILES);
$name2 = $file2['name'];
$type2 = $file2['type'];
$size2 = $file2['size'];
$tmppath2 = $file2['tmp_name'];
move_uploaded_file ($tmppath2, $path.'logos/'.$name2);
When I upload the files. The file name with $name2 is uploaded but the file with name $name1 is not uploaded
Please help why it not be uploaded
As you posted, the 'image' image has an error 4 code. That means no file was uploaded, as seen here:
http://www.php.net/manual/en/features.file-upload.errors.php
Why?
Since you are getting info in $_FILES, then you are not posting a total amount of data larger than the post_max_size php.ini directive, as, in this case, $_FILE would be completelly empty.
Then, maybe the php.ini upload_max_filesize, the maximum size for a singe file, is exceeded or the max_file_uploads is set to one, as max_file_uploads is the maximum number of files allowed to be uploaded simultaneously.
Check those params at http://php.net/manual/en/ini.core.php and in your php.ini file.
I tried this snippet in my localhost server, and placed in [www_root]/tests/uploads/test1.php (note I just used the relevant part for our case):
<?php
if (empty($_FILES)) {
echo "<form enctype='multipart/form-data' method='POST' action='http://localhost/tests/uploads/test1.php'>";
echo '<input id="file-sel" type="file" name="imaget" size="25" />';
echo '<input id="file-sel2" type="file" name="image" size="25" />';
echo '<button type="submit">Submit</button>';
} else {
echo print_r($_FILES);
}
And both files where correctly uploaded. Then the problem must be in the PHP configuration, not in the actual PHP code.
My values in php.ini:
post_max_size=8M
upload_max_filesize=2M
max_file_uploads=20
This means I can upload 20 files at the same time, but they can not exceed 8M in total nor an individual file can exceed 2M.
EDIT: after modifying the php.ini file, the server must be restarted in order to get those new values loaded.
Hope it helps.

upload a picture PHP (failing)

I'm trying to build a form to upload a thumbnail (image) to the server - looks like the image is successfully uploading to the servers temp folder (I've confirmed this with get_defined_vars - its giving a temp file name and no errors), but its not being moved to the folder in the DOCUMENT_ROOT
The end goal here is to get the image uploaded then return a url to be stored in MySQL
HTML Form (located in DOCUMENT_ROOT/admin)
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="thumbnail">Thumbnail </label>
<input type="file" name="thumbnail" id="thumbnail">
</form>
Upload.php
<?php
$thumbnail = uploadfile($_FILE['thumbnail']['name'],$_SERVER['DOCUMENT_ROOT'].'/thumbs/upload/',$_FILE['thumbnail']['tmp_name']);
if(($thumbnail)!== FALSE)
{echo $thumbnail;} else {echo 'upload failed<br>';}
function uploadfile($origin, $dest, $tmp_name)
{
$origin = strtolower(basename($origin));
$fulldest = $dest.$origin;
$filename = $origin;
echo '$fulldest '.$fulldest.'<br />';
echo '$filename '.$filename.'<br />';
if (move_uploaded_file($tmp_name, $fulldest))
{return $filename;}
return false;
}
?>
result (note: I've removed the actual document root)
$fulldest [DOCUMENT_ROOT] /thumbs/upload/
$filename
upload failed
I've also set an .htaccess directive for upload 500M to make sure image size isn't the problem
<IfModule mod_php4.c>
php_value upload_max_filesize 500M
php_value post_max_size 500M
</IfModule>
EDIT:: My target directory is chmod 0777 for testing - so permissions shouldn't be the problem.
I can't help noticing that your $filename is empty and $fulldest lack the filename. I think the problem is that you use $_FILE and not $_FILES.
$thumbnail = uploadfile($_FILES['thumbnail']['name'],
$_SERVER['DOCUMENT_ROOT'].'/thumbs/upload/',
$_FILES['thumbnail']['tmp_name']);
I think that would work.

Is there a predefined upload limit to $_FILES in PHP?

Hi I'm fairly new to HTML, PHP, MySQL etc.. I am wondering if there is a predefined upload limit using $_FILES. I ask because when I try to upload 8 images of around 1.5 megabytes the code does not work but when I upload 10 images of around 60 kilobytes the code works fine.
Here is my code and feel free to make any criticisms/comments about it:
</head>
<body>
<form action="test.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>
<?php
include 'connect.php';
if(!empty($_FILES['image']['tmp_name'])){
$allowed = array('jpg', 'gif', 'png', 'jpeg');
$count = 0;
foreach($_FILES['image']['name'] as $key => $name){
$image_name = $name;
$tmp = explode('.', $image_name);
$image_extn = strtolower(end($tmp)); //can only reference file
$image_temp = $_FILES['image']['tmp_name'][$count];
$count = $count +1;
if(in_array($image_extn, $allowed) === true){
$image_path = 'images/' . md5($image_name) . '.' . $image_extn;
move_uploaded_file($image_temp, $image_path);
mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image_path')") or die(mysql_error());
$lastid = mysql_insert_id();
$image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
$image_link = mysql_fetch_assoc($image_link);
$image_link = $image_link['image'];
$uploaded[] = $image_link;
}
else{
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
if(!empty($uploaded)){
foreach($uploaded as $new){
echo "<a href = $new>$new</a><p></p>";
}
}
else{
echo "Please select an image.";
}
?>
</body>
</html>
You are trying to upload an array of files, you will not be able to upload more than 20 files due to max_file_uploads limit in php.ini which is by default set to 20.
So you have to increase this limit to upload more than 20 files.
Note: max_file_uploads can NOT be changed outside php.ini. See PHP "Bug" #50684
Here are the settings you want to change in php.ini:
post_max_size
This setting controls the size of an HTTP post, and it needs to be set larger than the upload_max_filesize setting.
upload_max_filesize
This value sets the maximum size of an upload file.
Remember to restart your web server after making these changes.
Ref:
http://www.php.net/manual/en/ini.core.php#ini.post-max-size
http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
A few settings in your php.ini could be causing this. Look into memory_limit, post_max_size, upload_max_filesize. You could also be timing out. The best way to find out specifically is error_reporting(E_ALL);ini_set('display_errors','1');
In php.ini, there will be a post_max_size and upload_max_filesize directive.
You should also define a MAX_FILE_SIZE hidden input within your form, as per
http://www.php.net/manual/en/features.file-upload.post-method.php

Categories