I am working with the move_uploaded_file() function in PHP. I've managed to successfully troubleshoot a problem I was encountering, but I would like to be able to get the actual content of the warnning or error message. According to php.net (http://php.net/manual/en/function.move-uploaded-file.php) the move_uploaded_file() function returns FALSE and a warning on failure. I want the actual content of the warning, such as "failed to open stream: Permission denied..." so that I can record which errors are occurring. Is there a way to do this?
You can do using this: $_FILES['userfile']['error']
More info: http://php.net/manual/en/features.file-upload.errors.php
Related
I have a strange php issue.
I have a custom front-end post submission form for WordPress. It most of the time but the other times it seemingly at random doesn't work.
My PHP error log spits out the following:
PHP Warning: copy(): Filename cannot be empty in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 1071
PHP Warning: file_get_contents(https://ygoprodeck.com/pics/Trinity World Chalice-deck-14461.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 1025
PHP Warning: unlink(/home/y567889/public_html/pics/Trinity World Chalice-deck-14461.png): No such file or directory in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 172
So in theory, a user could submit 10 posts. Sometimes 2 of those posts would generate with errors (missing featured image etc etc) with the above error in the php log.
What I have tried:
Changed copy() to move_uploaded_file() with no joy.
Changed permissions on the "pics" folder to 775.
It occurs to one of my users in particular. It happens him with every post he attempts to upload. Generating a new WP account fixes this for him (although it still fails occasionally as per usual).
Because the error starts at line 1017 I'll show what the code is for that:
function moveYdktoDeck($ydkId,$postid){
$ydkUrl = wp_get_attachment_url( $ydkId );
$newfile = $_SERVER['DOCUMENT_ROOT'].'/UploadedDecks/3/'.$postid.'.ydk';
copy($ydkUrl, $newfile)
}
Function wp_get_attachment_url can return false. Check that before you use it in copy function.
I'm very new here but I'm facing an issue on one of my website with fopen().
$file="//".$_SERVER['SERVER_NAME']."/myfile".date("Ymd").".txt";
if (!file_exists($file)) {
$fp=fopen($file,"w+");
}
$fp=fopen($file,"r+");
And the error message :
fopen(mydomain/myfile20160629.txt): failed to open stream: No such file or
directory in /mydomain/myphpfile.php on line 24
What's funny is that I sometimes get an error message, sometimes not, depending what file use this. And I still got this message when the file is created...
Do you have any idea ?
Try set path : $file="//".$_SERVER['DOCUMENT_ROOT']."/myfile".date("Ymd").".txt";
but before that, move your file in directory $_SERVER['DOCUMENT_ROOT']
Problem: Any php file we attempt to access on our website shows up as a 500 internal server error. I'm not sure if this is related but I have had a look in the error logs and the below error appears:
[08-Nov-2013 12:41:51 UTC] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/htscanner.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20090626/htscanner.so:
cannot open shared object file: No such file or directory in Unknown on line 0
Attempt: Some of the things I have tried to do is delete the over size error log and renamed the htaccess file to see if that was causing the problem.
Page: You can see the problem at this page: http://science.org.au/support-us/donate-now.html (Half way down in the iframe)
Question: Does anyone have any ideas on how to fix this? Things to try?
Did you try this?
edit the file /etc/php5/cli/php.ini:
and remove the lines:
[htscanner] Extension = “htscanner.so”
config_file = “.htaccess”
default_docroot = “/var/www”
You should pay more attention to what your error log says. According to it, you should either install htscanner.so PHP extension, or remove reference to it from your php.ini.
Hello i have a stupid question i search in stackflow there was lots of answeserd but i couldnt find solution for my problem i want to call a file in php receiving error actually the code is correct but i am receiving a problem error which is:
Warning: require_once(1) [function.require-once]: failed to open stream:
No such file or directory in C:\xampp\htdocs\Upload images\upload.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '1'(include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Upload
images\upload.php on line 2
I am using dreamweaver. i deleted the path with directory and recreated with different name but not solved still prob exist. also i did google there also i didn't find solution ??????? any one please my head is exploding....??????????????????????????????
A quick test on my system reveals that require_once("file") or error_function(); fails with that exact message; require_once("file"); does not.
This is possibly because require_once is a statement rather than a function, and might have different parsing rules, causing your statement to actually be require_once('db.php' or mysql_error());. The PHP or operator with those arguments will return TRUE without calling mysql_error, causing the statement to be require_once(1) (1 == TRUE) - which would generate those messages. This is just my speculation.
tl;dr
Try removing the or mysql_error() from your code. require_once will generate a fatal error anyway. (Warning: Works On My Machine™.)
Try write complete path in require_once(path)
I wanna copy some doc files from a link. But, sometimes there's link that we don't have permission to access or the link that expired. For the link that we don't have permission, is there a solution?
and I wanna change the error message when it happen, the error message that I get is :
Warning: copy(http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/Web/People/ngm/15-721/summaries/12.pdf) [function.copy]: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in D:\AppServ\www\academicopter\functionWrapper.php on line 33
I've tried to change the error message with (or die ('CANNOT COPY')) the code below :
copy($Link, $savePath . basename($Link)) or die ('CANNOT COPY');
but, the error message still same warning ..... how to change it? thank you :)
You can muffle the warning using the # operator. Note that it would still run any custom error handler you may have configured.
if (false === #copy(...)) {
// copy failed
}
AFAIK there's no other way to get rid of the warning unless you use cURL to copy the file instead.
Edit
You could also use get_headers($url) and look for HTTP/x.x 403. It's one more request and personally I would save myself the trouble :)