I am developing a website that includes functions with file upload. This was working perfectly on localhost, but once I uploaded the site to my server it fails.
I have the following code to handle the file upload:
case "upload_to_gallery":
/*******************************
**** UPLOAD TO GALLERY ****
*******************************/
if ($con->checkLogin() && isset($_FILES['uploaded_files'])) {
$gallery_name = $_POST['gallery_name'];
for ($i = 0; $i < count($_FILES['uploaded_files']['name']); $i++) {
list($width, $height, $type, $attr) = getimagesize($_FILES["uploaded_files"]['tmp_name'][$i]);
if ($_FILES['uploaded_files']['type'][$i] == "image/png"
|| $_FILES['uploaded_files']['type'][$i] == "image/jpeg"
|| $_FILES['uploaded_files']['type'][$i] == "image/gif"
) {
$imgPath = "../files/img/galleries/" . $gallery_name . "/" . $_FILES['uploaded_files']['name'][$i];
$thumbPath = "../files/img/galleries/" . $gallery_name . "/thumbs/" . $_FILES['uploaded_files']['name'][$i];
move_uploaded_file($_FILES['uploaded_files']['tmp_name'][$i], strtolower($imgPath));
$filehandler->makeThumbnails($imgPath, $thumbPath, 300, 215);
}
}
header('location: '.MAINPATH.'/galleri/'.$gallery_name.'?billeder_uploadet');
And the errors i am getting, when trying to upload some files (images) on the server is:
Warning: move_uploaded_file(../files/img/galleries/test/1383204_10200900060289437_410542691_n.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /var/www/xn--cstanlg-rxa.dk/public_html/web/templates/formReceiverPost.php on line 35
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phptu17Hf' to '../files/img/galleries/test/1383204_10200900060289437_410542691_n.jpg' in /var/www/xn--cstanlg-rxa.dk/public_html/web/templates/formReceiverPost.php on line 35
Warning: getimagesize(../files/img/galleries/Test/1383204_10200900060289437_410542691_n.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /var/www/xn--cstanlg-rxa.dk/public_html/lib/class/FileHandler.php on line 158
Warning: Division by zero in /var/www/xn--cstanlg-rxa.dk/public_html/lib/class/FileHandler.php on line 159
Warning: Division by zero in /var/www/xn--cstanlg-rxa.dk/public_html/lib/class/FileHandler.php on line 159
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /var/www/xn--cstanlg-rxa.dk/public_html/lib/class/FileHandler.php on line 162
When I try to upload 0 files to the server it gives me this error:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /var/www/xn--cstanlg-rxa.dk/public_html/web/templates/formReceiverPost.php on line 26
Which I don't think makes sense, as I am checking if any files has been sent through..
I thought it might have something to do with permissions on the server, but all the relevant folders is set to '755'.
I hope someone might know what the problem could be.
Any help will be greatly appreciated.
Check carefully your errors.
Warning: move_uploaded_file(../files/img/galleries/test/1383204_10200900060289437_410542691_n.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /var/www/xn--cstanlg-rxa.dk/public_html/web/templates/formReceiverPost.php on line 35
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phptu17Hf' to '../files/img/galleries/test/1383204_10200900060289437_410542691_n.jpg' in /var/www/xn--cstanlg-rxa.dk/public_html/web/templates/formReceiverPost.php on line 35
Warning: getimagesize(../files/img/galleries/Test/1383204_10200900060289437_410542691_n.jpg) [function.getimagesize]: failed to open stream: No such file or directory in /var/www/xn--cstanlg-rxa.dk/public_html/lib/class/FileHandler.php on line 158
You have both test and Test in your paths.
Remember that Linux is case-sensitive for file and folder names. Windows is not. That might explain why it worked on your dev environment and why it didn't anymore on your prod server.
So pick either one of them and stick with it (I wouldn't recommend to use uppercase characters in your paths though).
Related
PHP - move_uploaded_file() unable to move /temp/ to actual folder in windows server. Where as it is working fine in linux without issue. I have even given read write permission for the folders.
if(empty($_FILES['verifyfile']['name']))
{
$path = 'No';
}
else
{
$name = $_FILES['verifyfile']['name'];
$tmp = $_FILES['verifyfile']['tmp_name'];
$path = "files/".basename($currentdatetime.$name);
move_uploaded_file($tmp,$path) ; // Moving Uploaded file
Error Log Given below
[14-Nov-2017 07:55:04 Asia/Kolkata] PHP Warning: move_uploaded_file(files/2017-11-14 07:55:04Sakthi APIL (Paid).pdf): failed to open stream: Invalid argument in C:\inetpub\wwwroot\verifydata.php on line 44 [14-Nov-2017 07:55:04 Asia/Kolkata] PHP Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php9779.tmp' to 'files/2017-11-14 07:55:04Sakthi APIL (Paid).pdf' in C:\inetpub\wwwroot\verifydata.php on line 44
I have photo upload on my site and it is working well when I was testing it on localhost. This is how and where the photo I uploaded on my localhost:
if($_FILES['file']['error'][$key] == 0 &&
move_uploaded_file($_FILES['file']['tmp_name'][$key],
"../gallery/{$gallery_images}"
)
){
....
}
But when I tried this on a webserver, it started showing errors. These are the errors:
Warning: move_uploaded_file(../gallery/289228065.jpg): failed to open stream: Permission denied in /var/www/alumni/admin/create_album.php on line 51
Warning: move_uploaded_file(): Unable to move '/tmp/php3hJjHL' to '../gallery/289228065.jpg' in /var/www/alumni/admin/create_album.php on line 51
Warning: move_uploaded_file(../gallery/2001593678.jpg): failed to open stream: Permission denied in /var/www/alumni/admin/create_album.php on line 51
Warning: move_uploaded_file(): Unable to move '/tmp/phpDiNyPg' to '../gallery/2001593678.jpg' in /var/www/alumni/admin/create_album.php on line 51
What should I do? I tried change the path but the same errors show up.
I have the following Warnings which SHOULD be there, but is there a way to stop them writting to the page without actually disabling Warnings globally?
Warnings:
Warning: file_get_contents(E:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(F:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(G:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(H:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(I:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(J:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(K:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(L:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(M:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(N:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(O:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
Warning: file_get_contents(P:/connected.txt): failed to open stream:
No such file or directory in C:\xampp\htdocs\ppa\test.php on line 19
PHP Function:
//Check if the connection file is present on each drive
function scanDrives() {
//Possible drives
$letters = "DEFGHIJKLMNOP";
$letters = str_split($letters);
$code = md5("FMbHSBTMTXhu3TWp");
//Check for a certain file on each device
foreach($letters as $x) {
$file = file_get_contents($x.":/connected.txt"); //This is what causes the error as the file can't be found.
if ($file != false) {
$file_code = split(":", $file);
//Check if the file has the correct pass code
if($file_code[0] == $code) {
//Successful, return pass value and device letter and set name
echo (1).",".$x.",".$file_code[1];
}
}
}
}
Thanks.
Don't just blindly open the file with file_get_contents. Check to see if it exists first.
if(file_exists($x.":/connected.txt")){
$file = file_get_contents($x.":/connected.txt");
// ...
}
I agree with #Rocket Hazmat,
but to answer your question - in PHP you can use Error Control operators .. Specifically you can use the # sign to ignore errors.
Simple Example
$file = #file_get_contents("file_doesnt_exist.php"); // wont throw any errors.
Try to put "#" before the function that gives you the warning!
http://us3.php.net/manual/en/language.operators.errorcontrol.php
I have a problem with a php code.
Folder name what-counter This folder contains a file with the following php code named counter.php also the hitcount.txt file.
<?php
$filename = '../what-counter/hitcount.txt';
$handle = fopen($filename, 'r');
$hits = trim(fgets($handle)) + 1;
fclose($handle);
$handle = fopen($filename, 'w');
fwrite($handle, $hits);
fclose($handle);
// Uncomment the next line (remove //) to display the number of hits on your page.
echo $hits;
?>
The following php code is used in root directory files also in files from folders which echo's the hits.
<?php include("../what-counter/counter.php"); ?>
The problem
The code works in the files in folders but not in the files that are directly in the root directory.
Example: index.php is in the root directory
Using this code i get this Warning
<?php include("what-counter/counter.php"); ?>
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 4
Warning: fgets(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 5
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 6
Warning: fopen(../what-counter/hitcount.txt) [function.fopen]: failed to open stream: No such file or directory in /home/what/public_html/what-counter/counter.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in /home/what/public_html/what-counter/counter.php on line 10
And using this code i get this Warning
<?php include("../what-counter/counter.php"); ?>
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include(../what-counter/counter.php) [function.include]: failed to open stream: No such file or directory in /home/what/public_html/include/footer.php on line 31
Warning: include() [function.include]: Failed opening '../what-counter/counter.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/what/public_html
/include/footer.php on line 31
What can i do to the $filename url and <?php include("../what-counter/counter.php"); ?> to get it to work in files in root directory as well as folders?
Either:
Include relative the the file: __DIR__.'/../somefile';/__DIR__.'/somepath/somefile';
Include relative to a known dir: $_SERVER['DOCUMENT_ROOT'] is often used, as is a PROJECT_DIR-like define() in your bootstrap code.
Change the working directory to a known/static dir chdir('/some/dir');. Not recommended as calling code may not expect this.
Sounds like absolute vs relative file problems...
Try replacing:
$filename = '../what-counter/hitcount.txt';
With, depending on where the file is located exactly, either of:
$filename = __DIR__ . '/what-counter/hitcount.txt';
$filename = dirname(__DIR__) . '/what-counter/hitcount.txt';
Or (if you're on an old php install) either of:
$filename = dirname(__FILE__) . '/what-counter/hitcount.txt';
$filename = dirname(dirname(__FILE__)) . '/what-counter/hitcount.txt';
$pid = mysql_insert_id();
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
exit();
}
I am using the above code to add images to a folder from a form on my webpage.
I keep getting the following error:
Warning: move_uploaded_file(../inventory_images/11.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/storeadmin/inventory_list.php on line 63
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpH51nOR' to '../inventory_images/11.jpg' in /Applications/XAMPP/xamppfiles/htdocs/storeadmin/inventory_list.php on line 63
Is this due to folder authentication?