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
Related
Im trying to add file uploading to my app but for some reason the php script doesnt move the file. I'm using angularjs and the code works fine but in the script it doesn't move/upload the file as it's supposed. Below is the code I have
<?php
$name = $_FILES['filee']['name'];
$tmp_name = $_FILES['filee']['tmp_name'];
$loc = '/media/img/';
if(move_uploaded_file($_FILES['filee']['name'], $loc)){
echo $_FILES['filee']['tmp_name'].$loc.$name;
}
?>
I don't see whats wrong with the script!!
you need to use tmp_name in the first argument of move_uploaded_file() function as below
<?php
$name = $_FILES['filee']['name'];
$tmp_name = $_FILES['filee']['tmp_name'];
$loc = 'media/img/'.$name; //desitination needs file name also
if(!is_dir('media/img/') && !file_exists('media/img') ) {
mkdir('media/img',0777,true);
}
if(move_uploaded_file($_FILES['filee']['tmp_name'], $loc)){
echo $_FILES['filee']['tmp_name'].$loc.$name;
}
?>
also make sure form has enctype attribute
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 move an uploaded file inside my server on my Cpanel hosting. I am using the PHP function move_uploaded_file, but how do I know the exact path of my Server?
I tried using this syntax:
move_uploaded_file($filelocation, http://website.com/thisfolder/filename.txt);
i do know that my file location variable is outputting correctly as I could see that it goes into var/ directory of the server.
I want the path in my filemanager that has the public_html folder.
/ServerName1/ServerName2/public_html/Folder1/thisfolder
How do I place it into the correct directory?
<?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");
}
}
?>
A sample code of how to upload to a directory. You can find more examples here : http://us1.php.net/move_uploaded_file
When I try to loop through the $_FILES array in PHP I get an error, because the filenames are all in unicode. Is it somehow possible to still accept files with unicode filenames?
Example code:
foreach ($_FILES["files"]["error"] as $key => $error)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
logz( "$tmp_name AND $path/$name" );
if ( $error == UPLOAD_ERR_OK )
{
move_uploaded_file($tmp_name, "$path/$name");
logz( "$tmp_name -> $path/$name" );
}
else
{
logz( "upload error" );
}
}
Pay attention to the log parameter; the output is:
/ -> path/%7B
The filename is not complete and $_FILES['files']['name'] is empty.
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]