fopen() with error on godaddy - php

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']

Related

PHP Zip Archive Extract Not working When Script is Executed by Crontab

Within my script I need to connect to ftp and download few zips files and then extract them in a directory. I have already got this implemented and it works as expected when I run the script through browser but when the crontab run this script it throws me error that it failed to extract file. I tried to research but I had no luck I don't understand the reason for this.
Below is the error that I am getting:
A PHP Error was encountered
Severity: Warning
Message ZipArchive::extractTo(/home/name/files/complete//filename.txt): failed to open stream: Permission denied
Filename: controllers/import.php
Line Number: 66
Script:
$txt_file: "/home/name/files/complete/Data.zip";
$server_txt_file = "/TXT/Data.zip";
if (ftp_get($ftp_conn, $txt_file, $server_txt_file, FTP_BINARY))
{
$zip = new ZipArchive;
if ($zip->open($txt_file) === TRUE)
{
$zip->extractTo('/home/name/files/complete/');
$zip->close();
unlink($txt_file);
}
}
I am trying to fix this for days now but I can not figure out what the issue is.
The error is telling you everything you need to know:
failed to open stream: Permission denied.
So your php process has no permission to write to the specified folder. So you should change the permissions of specified folder.

Error in imagecreatefromjpeg() function in PHP

When using the below php code it shows an error
<?php
$path="/hari002244/album/f41b6b54811c.jpg";
$src = imagecreatefromjpeg($path);
?>
It shows
SCREAM: Error suppression ignored for
Warning: imagecreatefromjpeg(/hari002244/album/f41b6b54811c.jpg): failed to open stream: No such file or directory in C:\wamp\www\newEmptyPHP.php on line 3
But when using
<?php
$path="f41b6b54811c.jpg";
$src = imagecreatefromjpeg($path);
?>
It doesn't show any error.
I had typed the path in URL bar and checked whether file exists.It is working perfectly.
Can you tell me why this error occurs? and also how to overcome this?
Thanks in advance.
The first path, "/hari002244/album/f41b6b54811c.jpg" is an absolute path, it starts from the root of the whole file system. Chances are, this path does not exist.
In the second case, it is just a filename "f41b6b54811c.jpg" which is relative to / located in the current working directory, which of course exists.
Got the answer
Instead of "/hari002244/album/f41b6b54811c.jpg"
Should be used "http://localhost/hari002244/album/f41b6b54811c.jpg"

Change error message when copy a file from a link

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 :)

Getting Content of PHP file upload error

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

thumbs.db messing up my upload routine

I'm getting the following error while uploading a zip archive.
Warning: ZipArchive::extractTo(C:\xampplite\htdocs\testsite/wp-content/themes/mytheme//styles\mytheme/Thumbs.db) [ziparchive.extractto]: failed to open stream: Permission denied in C:\xampplite\htdocs\testsite\wp-content\themes\mythem\uploader.php on line 17
The thing I can't quite figure is that I don't see a thumbs.db file in either the zip archive or the destination folder that was created (the upload still processes, I just get these errors).
The function is below, line 17 is commented...
function openZip($file_to_open) {
global $target;
$zip = new ZipArchive();
$x = $zip->open($file_to_open);
if($x === true) {
$zip->extractTo($target); //this is line 17
$zip->close();
unlink($file_to_open);
} else {
die("There was a problem. Please try again!");
}
}
Looks like just a warning. I would ignore it. I'm betting the error happens when it tried to overwrite the thumbs.db file in the extraction directory.
Alternatively, use getNameIndex to get a list of files, filter thumbs.db and use the second parameter of extractTo to filter the files.
http://www.php.net/manual/en/function.ziparchive-getnameindex.php
http://www.php.net/manual/en/function.ziparchive-extractto.php
This path is all messed up
C:\xampplite\htdocs\testsite/wp-content/themes/mytheme//styles\mytheme/Thumbs.db
I guess this might be windows issue.
can you add a line above line 17 saying
echo $file_to_open; exit;
and tell us what it says when you try to upload file?
Assuming that the mangled path works, then there is a thumbs.db in the destination directory (by default a hidden file), and/or your script does not have permission to write to that file/directory. Windows is very big on spitting out "permission denied" when a file is in use by another process, rather than saying "In use by process XXX".

Categories