Issues with Uploading Multiple files with PHP - php

I can see this question has been asked a million times before. I have been through many of the responses and can't seem to get it right:-
I am simply trying to upload multiple files. I'm certain that the form is correct. The issue I get is that if I use a foreach loop, PHP cycles through 5 times (I guess once for each key in $_FILES).
I have read that you should count the uploaded files in the $_FILE['file_upload'] array, then use a for loop, and include an index on the end, such as:-
$_FILES['file_upload']['name'][$1]
however, when I try to access those values I only get the first letter of the value (I think I understand why this is).
The only thing I can think is to use
for($i ; $i<$size ; $i++){...}
and then nest a foreach loop inside it, however, this seems inefficient and I've seen no other suggestions to this end.
I would therefore be eternally grateful if someone could set me straight once and for all. My code is here:-
foreach ($_FILES['file_upload'] as $key => $value){
$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
if(move_uploaded_file($tmp_file,$upload_location."/".$target_file)){
$message = "File uploaded successfully";
} else {
$error = $_FILES['file_upload']['error']; // get the error
$_SESSION['errors'][] = $error_msg[$error];// return the error that matches
}// end if
} // end for
So just to clarify - The above code works and uploads the image, but where the loop cycles through 5 times (I'm assuming once per $_FILES attribute), I am getting 5 error messages.I hope this makes sense.
Many thanks in advance for any pointers
Phill

The following was taken from: PHP Manual
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
Which in turn you should be able to modify to something like this:
<?php
$uploads_dir = '/uploads';
foreach ($_FILES["file_upload"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file_upload"]["tmp_name"][$key];
$name = $_FILES["file_upload"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>

change your foreach to this
foreach ($_FILES['file_upload']['tmp_name'] as $key => $value){
$tmp_file = $_FILES['file_upload']['tmp_name'][$key];
$target_file = basename($_FILES['file_upload']['name'][$key]);
if(move_uploaded_file($tmp_file,$upload_location."/".$target_file)){
$message = "File uploaded successfully";
} else {
$error = $_FILES['file_upload']['error'][$key]; // get the error
$_SESSION['errors'][] = $error_msg[$error];// return the error that matches
}// end if
} // end for

I dont think I understand you completely. If you are uploading multiple files, you should use foreach (no counter required).
The only counter you should use is to count the numbers of files that were successfuly uploaded.
Try this:
$success = 0;
foreach ($_FILES['files']['name'] as $file => $name){
$tmp_file = $_FILES["files"]["tmp_name"][$file];
$target_file = $name;
if(move_uploaded_file($tmp_file,$upload_location."/".$target_file)){
$message = "File uploaded successfully";
$success++;
} else {
$error = $_FILES['file_upload']['error']; // get the error
$_SESSION['errors'][] = $error_msg[$error];// return the error that matches
}// end if
} // end for
echo $success.' files were uploaded';

Related

Upload fails "move uploaded file"

First off, the upload folder is given 777, and my old upload script works, so the server accepts files. How ever this is a new destination.
I use krajee bootstrap upload to send the files. And I receive a Jason response. The error seems to be around move uploaded file. I bet it's a simple error from my side, but I can't see it.
<?php
if (empty($_FILES['filer42'])) {
echo json_encode(['error'=>'No files found for upload.']);
// or you can throw an exception
return; // terminate
}
// get the files posted
$images = $_FILES['filer42'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($_FILES["filer42"]["tmp_name"][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {.
$output = [];
$output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
I think field name is the issue. Because you are getting image name with filer42 and upload time, you are using pictures.
Please change
$_FILES["pictures"]["tmp_name"][$i]
to
$_FILES["filer42"]["tmp_name"][$i]
And check now, Hope it will work. Let me know if you still get issue.
The error is not in this script but in the post.
I was using <input id="filer42" name="filer42" type="file">
but it have to be <input id="filer42" name="filer42[]" type="file" multiple>
as the script seems to need an arrey.
It works just fine now.

scandir() display file found/not found error messge

I have 100 files and I am scanning them and picking the correct file out of them.
I am using the following code:
$dir = 'myDir';
$files1 = scandir($dir);
$scanned_directory = array_diff($files1, array('..', '.'));
foreach ($scanned_directory as $key => $value) {
$onlyname=explode(".", $value);
if($onlyname[0]== $name){
// echo "file found";
break;
}else{
//echo "<h2>Not Found. Please Try Later</h2>";
}
}
The problem with this is that if the file is the 10th file I get 9x not found, before I get the file found message.
What is the proper way to display error message if no file is found?
I simplified your code a bit.
First of all I get all files from your directory into an array with glob(). Then I simply grab all files which have the name $name with preg_grep() and check with count() if there is at least 1 file with that specific name.
<?php
$dir = "myDir";
$files = glob($dir . "/*.*");
if(count(preg_grep("/^$name\..*$/", array_map("basename", $files))) > 0)
echo "file found";
else
echo "<h2>Not Found. Please Try Later</h2>";
?>

Passing uploaded files to another part of the script for onward processing

I have searched the forum but the closest question which is about the control stream did not help or I did not understand so I want to ask a different question.
I have an html form which uploads multiples files to a directory. The upload manager that handles the upload resides in the same script with a different code which I need to pass the file names to for processing.
The problem is that the files get uploaded but they don't get processed by the the other code. I am not sure about the right way to pass the $_FILES['uploadedFile']['tmp_name']) in the adjoining code so the files can be processed with the remaining code. Please find below the script.
More specif explanation:
this script does specifically 2 things. the first part handles file uploads and the second part starting from the italised comment extracts data from the numerous uploaded files. This part has a variable $_infile which is array which is suppose to get the uploaded files. I need to pass the files into this array. so far I struggled and did this: $inFiles = ($_FILES['uploadedFile']['tmp_name']); which is not working. You can see it also in the full code sample below. there is no error but the files are not passed and they are not processed after uploading.
<?php
// This part uploads text files
if (isset($_POST['uploadfiles'])) {
if (isset($_POST['uploadfiles'])) {
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/Uploads/';
for ($i = 0; $i < count($_FILES['uploadedFile']['name']); $i++) {
//$number_of_file_fields++;
if ($_FILES['uploadedFile']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['uploadedFile']['name'][$i];
//if (is_uploaded_file($_FILES['uploadedFile']['name'])){
if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'][$i], $upload_directory . $_FILES['uploadedFile']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
echo "Files successfully uploaded . <br/>" ;
echo "Number of files submitted $number_of_uploaded_files . <br/>";
echo "Number of successfully moved files $number_of_moved_files . <br/>";
echo "File Names are <br/>" . implode(',', $uploaded_files);
*/* This is the start of a script to accept the uploaded into another array of it own for* processing.*/
$searchCriteria = array('$GPRMC');
//creating a reference for multiple text files in an array
**$inFiles = ($_FILES['uploadedFile']['tmp_name']);**
$outFile = fopen("outputRMC.txt", "w");
$outFile2 = fopen("outputGGA.txt", "w");
//processing individual files in the array called $inFiles via foreach loop
if (is_array($inFiles)) {
foreach($inFiles as $inFileName) {
$numLines = 1;
//opening the input file
$inFiles = fopen($inFileName,"r");
//This line below initially was used to obtain the the output of each textfile processed.
//dirname($inFileName).basename($inFileName,'.txt').'_out.txt',"w");
//reading the inFile line by line and outputting the line if searchCriteria is met
while(!feof($inFiles)) {
$line = fgets($inFiles);
$lineTokens = explode(',',$line);
if(in_array($lineTokens[0],$searchCriteria)) {
if (fwrite($outFile,$line)===FALSE){
echo "Problem w*riting to file\n";
}
$numLines++;
}
// Defining search criteria for $GPGGA
$lineTokens = explode(',',$line);
$searchCriteria2 = array('$GPGGA');
if(in_array($lineTokens[0],$searchCriteria2)) {
if (fwrite($outFile2,$line)===FALSE){
echo "Problem writing to file\n";
}
}
}
}
echo "<p>For the file ".$inFileName." read ".$numLines;
//close the in files
fclose($_FILES['uploadedFile']['tmp_name']);
fflush($outFile);
fflush($outFile2);
}
fclose($outFile);
fclose($outFile2);
}
?>
Try this upload class instead and see if it helps:
To use it simply Upload::files('/to/this/directory/');
It returns an array of file names that where uploaded. (it may rename the file if it already exists in the upload directory)
class Upload {
public static function file($file, $directory) {
if (!is_dir($directory)) {
if (!#mkdir($directory)) {
throw new Exception('Upload directory does not exists and could not be created');
}
if (!#chmod($directory, 0777)) {
throw new Exception('Could not modify upload directory permissions');
}
}
if ($file['error'] != 0) {
throw new Exception('Error uploading file: '.$file['error']);
}
$file_name = $directory.$file['name'];
$i = 2;
while (file_exists($file_name)) {
$parts = explode('.', $file['name']);
$parts[0] .= '('.$i.')';
$new_file_name = $directory.implode('.', $parts);
if (!file_exists($new_file_name)) {
$file_name = $new_file_name;
}
$i++;
}
if (!#move_uploaded_file($file['tmp_name'], $file_name)) {
throw new Exception('Could not move uploaded file ('.$file['tmp_name'].') to: '.$file_name);
}
if (!#chmod($file_name, 0777)) {
throw new Exception('Could not modify uploaded file ('.$file_name.') permissions');
}
return $file_name;
}
public static function files($directory) {
if (sizeof($_FILES) > 0) {
$uploads = array();
foreach ($_FILES as $file) {
if (!is_uploaded_file($file['tmp_name'])) {
continue;
}
$file_name = static::file($file, $directory);
array_push($uploads, $file_name);
}
return $uploads;
}
return null;
}
}

File upload PHP problem

Got this working
$i = 0;
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];
$image_name = $_FILES["image"]["name"][$key];
$image.$i = move_uploaded_file($tmp_name, "uploads/$image_name");
$i ++;
}
}
I cant seem to get the directory storing into the variables $image# any ideas?
Why are oyu mixing usage of $_FILES and $HTTP_POST_FILES? Usage of the later suggests that you're using an old and outdated tutorial.
You'RE also not checking whether multiple files have been successfully transefered and using copy() for this purpose is not encouraged.
See move_uploaded_files() which has an example about handling multiple uploads.
I got this working in the end by creating an array and storing the values in this array
Shouldn't it be
$_FILES['image'][$i]['name']
Rather than
$_FILES['image']['name'][$i]

How to get the total number of iterations in a foreach

There may be a better way of doing this and I'm certainly open to suggestions.
I have a file upload script that will handle multiple uploads. What I want to do is count the number of iterations that the loop makes for each file that was successfully moved and if that number equals the total number of files uploaded, then use an exception to show the user that the files were received.
I thought that I would increment inside the loop then count from there but what I am getting is an array for each file that is uploaded which results in an incorrect total. Is there a better way to do this or successfully count each iteration?
This is the structure I have to work with
foreach($files as $file)
{
if ($file['error'] == UPLOAD_ERR_OK)
{
move_uploaded_file($file['tmp_name'], $filename);
}
else
{
//error
}
}
You pretty much have to do it with a counter.
$success = 0;
foreach($_FILES as $file) {
if(is_uploaded_file($file['tmp_name'])) {
move_uploaded_file($file['tmp_name'], $destination);
$success += 1;
}
}
if($success != count($_FILES)) {
//error message / exception
}
Edit - You can set an error flag, or flags in your error handling... but there's not really a way to do this that is insanely better.
foreach($files as $file)
{
if ($file['error'] == UPLOAD_ERR_OK)
{
move_uploaded_file($file['tmp_name'], $filename);
}
else
{
//error
$upload_errors += 1;
//or, to get a little more info...
//$upload_errors[] = $file
}
}
if( $upload_errors == 0) { //or count($upload_errors) == 0
// tell the user that the upload failed.
}
foreach ($array as &$item)
You can just do count($array)
Just throwing this out there since you know the number of files how but just using a for loop. with two counters one for the loop and one for successes, and unless your script fails mostly ( and you want to not how unusual it is that everything worked) don't throw an exception if everything works.
Here you can see that you can use count($_FILES) to count the number of uploaded files. Mind you, this is not the number of correctly uploaded files.
<?php
if (isset($_FILES) && !empty($_FILES)) {
echo count($_FILES).' files were uploaded<br>';
?><pre><?php
print_r($_FILES);
?></pre><?php
}
?>
<form
action="<?php echo $_SERVER['PHP_SELF'];?>"
method="post"
enctype="multipart/form-data"
>
File 1<input type="file" name="file1"><br>
File 2<input type="file" name="file2"><br>
File 3<input type="file" name="file3"><br>
<input type="submit" value="upload">
</form>

Categories