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]
Related
I have a classic php script for saving tmp files:
$uploads_dir = "scans";
var_dump( $_FILES );
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
rename($tmp_name, urldecode("/var/www/html/$uploads_dir/$name") );
}
}
echo urldecode("/var/www/html/$uploads_dir/$name");
However, the file 'фавикон.png' gets saved as 'фавикон.png'. Please help me out what to do with encoding.
Thank you
EDIT:
Got it working with iconv function. However, for some weird reason it had to be encoded into windows format.
The resulting code:
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
$name =iconv('UTF-8','windows-1251', $name);
copy($tmp_name, "/var/www/html/$uploads_dir/$name");
}
You might need the following setlocale(LC_ALL, 'ru_RU.utf8')
try to put this line on the top of the script:
header('Content-Type: text/html; charset=utf-8');
Have you try to use utf8_decode:
utf8_decode($name);
I suggest that you change the name of the file, there are very good reasons to do this. Check this post for more details: https://stackoverflow.com/a/17866898/1016425
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';
I'm trying to import data from multiple .csv in a web app thanks to a single input, but I'm struggling manipulating $_FILES.
All I want to do is separating files into two arrays according to their names. I'm not able to upload these files on the server.
This is the input in the form page used to upload files :
<input type="file" name="file[]" id="inputrapports" accept=".csv" multiple="true"/>
In my action page, i'm doing this :
$files=$_FILES['file'];
$q=array();
$c=array();
foreach($files as $file){
if(strtoupper(substr(str_replace(' ','',$file['name']),7,1))=="Q"){
$q[]=$file;
}else{
$c[]=$file;
}
}
Unfortunately, it doesn't work. So I tried this :
foreach($files['name'] as $f){
echo $f; //I'm getting the name
}
foreach($files as $f){
echo $f['name']; //I'm not
}
I could use the first attempt shown above, but in this case I won't be able to put the entire array of the file in the appropriate array.
Can you explain me why this two attempts give different answers ?
Could you help me find a solution ?
Thanks in advance.
The dirty way to solve this is:
$files = $_FILES['file'];
for($i=0;$i<count($files['tmp_name']); $i++)
{
echo $files['name'][$i];
}
Another approach is from the php documentation:
function diverse_array($vector) {
$result = array();
foreach($vector as $key1 => $value1)
foreach($value1 as $key2 => $value2)
$result[$key2][$key1] = $value2;
return $result;
}
$files = diverse_array($_FILES["file"]);
And then proceed with your first foreach.
$q=array();
$c=array();
foreach($files as $file){
if(strtoupper(substr(str_replace(' ','',$file['name']),7,1))=="Q"){
$q[]=$file;
}else{
$c[]=$file;
}
}
I am trying to upload multiple images into the folder using php . The code can print out the file names which means I get the files but now it does not upload them and I get no error : below is my code
<?php
$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
foreach($_FILES['FILE_NAME']['tmp_name']as $key => $error ){
print_r($key);
$file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
#print image names
echo $file_upload.'</br>';
move_uploaded_file($file_upload,$target);
}
}
?>
In target you have to give the file name too. Please use the code below,
$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
foreach($_FILES['FILE_NAME']['tmp_name'] as $key => $error ){
print_r($key);
$file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
print image names
echo $file_upload.'</br>';
move_uploaded_file($file_upload,$target.$_FILES['FILE_NAME']['name']);
}
}
I think the problem is in the foreach loop.
foreach ($_FILES['FILE_NAME']['tmp_name'] as $key => $val) {
// this loops through the tmp_name of $_FILES['FILE_NAME']
// which is a string
}
I think you meant something like:
foreach ($_FILES as $index => $fileArray) {
$tmpName = $fileArray['tmp_name'];
echo "File at key $index is temporarily uploaded at $tmpName";
}
The code above will loop through all uploaded files and print it's current filename.
It might happen that your target folder is not writable.
I also think, that the cause of which you're not getting errors is, that you have the following:
print_r($key);
Yous should have:
print_r($error);
There can be multiple reasons for this :
The target folder must exist before trying to move the file from temp location to the target and must also be writable
the move_uploaded_file takes the second argument as the file name followed by the directory name, so it can be something like : target folder/user.file.name.ext
If you are uploading multiple files, then the $_FILES must be accessed as shown in the link : http://php.net/manual/en/features.file-upload.multiple.php
for the php error messages that you may encounter, here is a list : http://php.net/manual/en/features.file-upload.errors.php
As a bit of a follow up to Javascript form won't submit (to view the code I am using visit that link) I am now encountering a problem that I cannot find the file that has been uploaded.
I have added $files = apc_fetch('files_'.$_POST['APC_UPLOAD_PROGRESS']); to the top of my page and this is the output of print_r($files);
Array
(
[theFile] => Array
(
[name] => tt1.mp4
[type] => video/mp4
[tmp_name] => /tmp/php2BEvy7
[error] => 0
[size] => 1050290
)
)
However when I try to run the following code:
if (file_exists($files['theFile']['tmp_name'])) {
$webinarType = strcmp($files['theFile']['type'], 'video/mp4');
if($webinarType == 0) {
$webinarFile = $fileTitle;
$webinarTempName = $files['theFile']['tmp_name'];
} else {
echo 'Webinar must be .mp4';
}
} else {
echo "No File";
}
I get the No File output.
I have ssh'd into the server and the file is not in /tmp/, /path/to/public_html/tmp/ or path/to/file/tmp/ all of which exist.
I have tried to use move_uploaded_file() but as this is executed on all file inputs I can't get the tmp_name dynamically due to my limited knowledge of javascript.
tl;dr version; Where is my file gone and how can I find it?
NOTE; This form did work before the APC intevention and I am running wordpress in case that affects anything.
Fixed this one on my own as well.
In the progress.php file (found on the other question) I modified the elseif statement with this:
elseif(($s_progressId = $_POST['APC_UPLOAD_PROGRESS']) || ($s_progressId = $_GET['APC_UPLOAD_PROGRESS']))
{
// If the file has finished uploading add content to APC cache
$realpath = realpath($PHP_SELF);
$uploaddir = $realpath . '/tmp/';
foreach ($_FILES as $file) {
if(!empty($file['name'])) {
$uploaded_file = $file['name'];
$moveme = $uploaddir.$uploaded_file;
move_uploaded_file($file['tmp_name'], $moveme);
}
}
apc_store('files_'.$s_progressId, $_FILES);
die();
}
That way I could iterate through the $_FILES array without knowing the name of the input. I noticed that it loops through a couple of times hence the if(!empty()) however in hindsight it's probably best practice anyway.