I am adding attachments as follows:
for ($i = 0; $i <= 2; $i++)
{
if(file_exists($dir . $_FILES["file".$i]["tmp_name"])){
$mail->AddAttachment($dir . $_FILES["file".$i]["tmp_name"],$_FILES["file".$i]["name"]);
}
}
Files are uploaded on server properly, but only first attachment is attached to the email. For second and third attachment I get error: Could not access file: upload/. I have have found that line 7 => 0 in class.phpmailer.php should be replaced with 7 => count($this->attachment) but it did not help. Can you help me with this issue? TIA
If you're getting an error "Could not access file: upload/", that suggests that $_FILES["file1"]["tmp_name"] and $_FILES["file2"]["tmp_name"] are both empty/blank, and contain no values (otherwise it would say "Could not access file: upload/foo.gif").
echo out these values to the screen and see if they're actually there. Better yet, use print_r( $_FILES ); to see all the values in this array.
I suspect file_exists would return true even though the file names are blank, because file_exists also works on folders (i.e. file_exists is telling you the "upload/" folder exists).
EDIT: One other thing to mention, if you're using PHP 5.2.12 or better, check that max_file_uploads in your INI settings isn't preventing you from uploading more than one file.
Related
I've got a php file setup for uploading images to an Amazon server via AWS. I've come to an error message which strangely is showing up on my prod server (the amazon server) but not my dev server (just a regular php server). The Amazon server has previously thrown errors when something is not structured exactly how it wants. For example if a an a tag has a href /home it will lead to an error page unless I change the tag to /home/.
Anyway i''ve narrowed down in my PHP script where the error lies and my php function move_uploaded_fileis evaluating to false and stopping my file from beginning the upload to Amazon s3 (at this stage the file is on the server where the site is hosted but not yet on the s3). Here is my if statement with some variables declared above that should be evaluating to true:
$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file)) {
//upload to s3
} else {
//error
}
I inserted the following code above the if statement to see what it was spitting out and here's what it evaluated in comments under it:
echo json_encode($target_file);
// "..\/uploads\/Grad.jpeg"
echo $_FILES["fileToUpload"]["tmp_name"];
// /tmp/phpQA1667
echo json_encode(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file));
// false
I'm no php expert and can't seem to put my finder on why it's evaluating to false. Can anyone see where i've gone wrong?
move_uploaded_file returns false in two cases:
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
Either way, you should be getting an error back in your $_FILES superglobal.
Do a print_r($_FILES) and have a look at any error messages it gives you, and whether or not the array is populated.
Once you find that error, it should be easy for you to determine the exact cause of the issue.
A comprehensive list of the errors can be found at http://php.net/manual/en/features.file-upload.errors.php
I'm just having some trouble working with $_FILES and file_get_contents.
$nomFichier = $_FILES['pieceJointe']['name'];
echo is_readable($_FILES['pieceJointe']['tmp_name']);
echo $_FILES['pieceJointe']['error'];
if(file_get_contents($_FILES['pieceJointe']['tmp_name']) == false)
{
echo "impossible de lire le fichier.<br/>";
}
else
{
// store the file into a BLOB field in my database
}
is_readable display "1", $_FILES['pieceJointe']['error'] says 0.
But file_get_contents return false.
I notice that happen only with files which the name contains accents.
Everything is working fine with only letters/numbers file names.
Do I missed something ?
ps: I'm french student, not professional, sorry for my english :o
Thx!
Use var_dump to see the actual value of is_readable(). The "1" that you see is actually the output of $_FILES['pieceJointe']['error'] which means the file you are trying to upload is too large. See http://www.php.net/manual/en/features.file-upload.errors.php.
I guess that there is some conflict of file_get_contents function and open_basedir directive of your php.ini file:
http://www.php.net/manual/en/ini.core.php#ini.open-basedir
Try to use move_uploaded_file function before you read the file.
It will move uploaded file from temporary directory of your server to specified directory, (open_basedir directive only effects the destination parameter of move_uploaded_file)
Isn't $_FILES['pieceJointe']['tmp_name'] just a file name name, instead of a valid full path?
Maybe move the file first with move_uploaded_file before opening it.
i try to upload files into my server also to my database.
However, i cannot upload them.
I can see my two files with below code,
echo $_FILES['file']['name'][0];
echo $_FILES['file']['name'][1];
$fileName = $_FILES['file']['name'][0];
$add = "img01.imgsinemalar.com/images/haber_anasayfa/" . $fileName;
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
NCore::db('NEWS')->insertAsArray(array('CONTENT' => $_POST['TvSpotdesc'], 'ADD_DATE' => $date, 'TITLE' => $_POST['newsTitle'],IMAGE_MAIN => $add))->execute();
echo "Haber başarıyla gönderildi.";
} else {
echo "Failed to upload file Contact Site admin to fix the problem";
}else {
echo $msg;
}
But above code does not upload my files, it goes directly to the else statement, why?
Failed to upload file Contact Site admin to fix the problem
Your code should be outputting two notices and a warning from this line:
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
The two notices would be because you have not quoted file_up and tmp_name and the warning to inform you of the reason move_uploaded_file has failed, as documented. You should check out the logs to see these for yourself (development without seeing any error messages is not fun).
The reason it fails is that you cannot move a file to a remote server -- $add is not a local path.
Rename $_FILES[file_up][tmp_name] to $_FILES['file']['tmp_name']
There is no such variable as file_up as per the code you pasted. Also, files can only be stored on the same server. so gove relative path like /home/www/myapp/images/ instead of server name.
I'm writing a PHP application and in my code i want to create create and return images to the browser. However, sometimes i'm getting some weird results where the image cannot be created since the file does not seem to exist.
Here is a sample error message I get and the code in a nutshell. I do know that the image exists, but still the method sometimes fails, and sometimes it succeeds, even for the same file.
The error:
Warning: imagecreatefrompng(path/to/image.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in file test.php on line 301
The code:
if (file_exists($filename)) {
$image = imagecreatefrompng($filename);
}
I would greatly appreciate any hints or tips of what might be wrong and how I can improve the code to be more stabile.
I suggest you use is_readable
if (is_readable($filename)) {
$image = imagecreatefrompng($filename);
}
The file may "exist" but is the file accessible? what does file_exists actually do?
if it opens the file and then closes it make sure that the file is actualy closed and not locked before imagecreatedfrompng fires.
it would be a good idea to try catching the error in a loop and make 4 or 5 attempts before handing back a controlled error.
maybe try is_readable() or is_writable() instead?
Have you considered checking for the correct permissions? If the file cannot be read, but the directory can, you would get file_exists(...) = true, but would not be able to open a handle to the file.
Use is_readable() to check whatever you have permission to access that file.
You can try GD :
IF($img = #GETIMAGESIZE("testimage.gif")){
ECHO "image exists";
}ELSE{
ECHO "image does not exist";
}
bro check for white spaces in your filepath. I recently had this issue while i was tring to include a file from a module i was creating for an app. Other modules included well when called but one didnt. It turned out that there was a white space in the filepath. I suggest u try php trim() function. If this works holla.
I'm having an extremely weird problem with a PHP script of mine.
I'm uploading a couple of files and having PHP put them all in one folder.
I've have trouble with random files being sent and random ones not being sent. So I debugged it and I got a very weird result from the $_FILES[] array.
I tried it with 3 files.
$_FILES["addFile"]["name"] Holds the names of the 3 files.
You'd expect $_FILES["addFile"]["tmp_name"] to hold the 3 temporary names that PHP uses to copy the files, but it doesn't. It holds just one name. The other 2 are empty strings, which generate an error whilst uploading(which I supress from being displayed)
This is very odd. I've tried mulitple situations and it just keeps on happening.
This must be something in my settings or perhaps even my code.
Here's my code:
$i = 0;
if (!empty($_FILES['addFile'])) {
foreach($_FILES['addFile'] as $addFile) {
$fileToCopy = $_FILES["addFile"]["tmp_name"][$i];
$fileName = $_FILES["addFile"]["name"][$i];
$i++;
if(!empty($fileToCopy)){
$copyTo = $baseDir."/".$fileName;
#copy($fileToCopy, $copyTo) or die("cannot copy ".$fileToCopy." to ".$copyTo);
}
}
exit(0);
}
Since the tmp_name is empty, the if-value will be false so it's gonna skip the die() function.
Does anybody know what might be causing this?
further info: I'm using Windows XP, running WAMP server. Never had this problem before and I can acces all maps from which I've tried to upload. Security settings of windows can't be the issue I think.
I'm sorry but it seams to me that you are trying to upload all 3 files with the same variable name? Is this right?
But this will not work because they will overwrite each other.
I think the better an cleaner way it would be to use something like
$i = 0;
foreach($_FILES['addFile'.$i] as $addFile) {
if(!empty($addFiles) {
move_uploaded_file($addFile['temp_name'], 'YOUR DIRECTORY');
}
$i++;
}
Relevent, but probably not going to help: but move_uploaded_file is a (slightly) better way to handle uploaded files than copy.
Are any of the files large? PHP has limits on the filesize and the time it can take to upload them ...
Better to send you here than attempt to write up what it says:
http://uk3.php.net/manual/en/features.file-upload.common-pitfalls.php
Your loop logic is incorrect. You are using a foreach loop on the file input name directly, which stores several properties that are of no interest to you ('type','size', etc).
You should get the file count from the first file and use it as the loop length:
if (!empty($_FILES['addFile']) && is_array($_FILES['addFile']['name'])) {
$length = count($_FILES['addFile']['name']);
for($i = 0; $i < $length; $i++) {
$result = move_uploaded_file($_FILES['addFile']['tmp_name'][$i],$baseDir."/" . $_FILES['addFile']['name'][$i]);
if($result === false) {
echo 'File upload failed. The following error has occurred: ' . $_FILES['addFile']['error'][$i];
}
}
}
Check the error code if you are still having problems, it should provide all the information you need to debug it.