I have the following code:
$cachefile = "http://www.DOMAIN.com/users/{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file path is not valid, as it produces this error: Warning: chmod() [function.chmod]: No such file or directory in ...
However, this code does:
$cachefile = "{$user}.php";
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_clean(); // Send the output to the browser
chmod($cachefile, 0644);
The file--using the second block of code--is created in the same folder as the script that created it. However, I would like to save it to the first block's location. I look on php.net for fopen and I didn't see that I was doing anything wrong, but obviously I am.
EDIT (comment response):
I also tried $cachefile = $_SERVER["DOCUMENT_ROOT"] . "/users/{$user}.php" to no avail.
You cannot write to a remote file using fwrite(). If you're writing to a file that is on your own server, use a relative path.
You need to use the full path to the file on the server, not the web address:
$cachefile = "/var/www/path/to/users/{$user}.php";
You cannot open remote files, you can only edit files on the local filesystem, if the file is on the file system, then you have to use the relative path,
Also instead of using fopen, you could just use file_get_contents() and file_put_contents().
Related
I want to open a ZIP file by passing a remote URL (http://www.example.com/file.zip or http://localhost/wordpress/wp-content/uploads/file.zip) instead of a file location (C:\wamp\www\wordpress\wp-content\uploads\file.zip)
This constructor works fine for a file location but not for a remote url of a file. How does one open a file using a remote URL for this scenario?
public function __construct($file = false)
{
if ($file && is_file($file)) {
//$file="C:\wamp\www\wordpress\wp-content\uploads\file.zip" here
$this->open($file);
$this->fileName = basename($this->filePath = $file);
} else {
throw new \Exception($file . " not a regular file");
}
}
The safest way is to
download the file
This is super easy if allow_url_fopen is enabled: file_get_contents() accepts remote URLs. If that's not enabled, use cURL or a Wordpress HTTP helper to download it.
save it locally
Also super easy, with file_put_contents(). The /tmp folder is probably writable for you. On Windows, I don't know where the tmp folder lives.
open it like any other
As you would a local ZIP archive, with ZipArchive::open() or your nameless class
just use php fopen function
http://php.net/manual/en/function.fopen.php
$handle = fopen("http://www.example.com/", "r");
I have used this to get the contents of a webpage but not a zip file - not sure if that will work but it did well for me. $contents definitely worked for text.
// For PHP 5 and up
$handle = fopen("https://www.thesiteyouwant.com/the_target_file.ext", "r");
$contents = stream_get_contents($handle);
http://php.net/manual/en/function.stream-get-contents.php
I cannot update any txt files using php. When I write a simple code like the following:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
Both files do exist in the folder. I just cannot see any updates on bob.txt.
Is this a permission error in windows? It works fine on my laptop at home. I also cannot change the php files on my website, using filezilla.
It may very well be a file permissions issue.
Try your code using a direct pointer to the file instead of a path to it, using the following code:
I added a chmod directive. See the comments above chmod ($file, 0644);
Tested succesfully on my hosted WWW website:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
Perhaps you have to set the permission from bob.txt to 0777 (or something else). In FileZilla it is very easy, because you can just check the permissions you want.
I have a vb.net app that webrequests a PHP file which does this:
<?php
$msg = $_GET['w'];
$logfile= 'savedrv.idps';
$fp = fopen($logfile, "w");
fwrite($fp, $msg);
fclose($fp);
?>
I want to make a PHP file that will open the new file created "savedrv.idps" so I can read it in vb.net. This is what I tried:
<?php
$logfile= 'reg.idps';
$fp = fopen($logfile, "r");
fclose($fp);
?>
How can I accomplish this?
Most likely your IIS settings for this Virtual Directory forbid a file with this extension to be browsed to. I got 404.7 error attempting to open a file in IE when I browsed to the URL: http://localhost/mysite/myvbfile.vb Using your browser, try to open the same URL that your VB program is attempting to access. I anticipate that you will get the 404.7 error in the browser window too.
You have two approaches here:
Have your PHP script write the file to a location outside of IIS where your VB.Net program can access.
Modify the Request Filtering in IIS for your site so that this file can be browsed.
(screenshot) http://support.citrix.com/article/html/images/1CTX132655-1.gif
I have a zip file into a directory like this:
drwxr-xr-x 2 salome salome 4096 Dec 16 17:41 staff.zip
When I unzip the file with ZipArchive class, all the upzipped files are owner by nobody user. Is there a way to avoid this owner change?
I am able to use ftp if it is required (only as salome user).
This script eventually will be shared at multiple hosts, so the idea is keep it as generic as possible.
You might consider extending the zipArchive class and override the extractTo method to also perform a chown() on the files in the directory.
Based on the use case you discussed in the comments, you also might want to consider the use of the Phar archive format. php.net/manual/en/intro.phar.php
Phar's would allow your module submitters to submit a file file of executable PHP code that you would not need to extract at all.
Ok, I have resolved the problem of nobody user. I will try to explain all my workaround.
#Mike Brant's answer
Mike suggest to me make use of chown() function overriding extractTo() method. Well, before to willing with it, I tested the chown() function standalone constantly it printed the error:
failed to create stream: Permission denied in ...
Looks like chown will not work for the major shared hostings
FTP functions
So, keeping going I though that FTP functions I made a script that works fine, at least for now xD. This is a resume what the script does for one zipped file:
Create a temp file using tmpfile().
Using ftp_fput() to put the temp file in the current directory with the zipped file.
Give write permissions using ftp_site and CHMOD 0777.
Read the zipped file content with $content = $zip->getFromName('zipped-file.txt');.
Put the content to the new file using fputs($fp, $content);.
Close connections
The code below illustrates the complete process
$zip = new ZipArchive;
$ftp_path_to_unzip = '/public_html/ejemplos/php/ftp/upload/';
$local_path_to_unzip = '/home/user/public_html/ejemplos/php/ftp/upload/';
if ($zip->open('test.zip') == TRUE) {
//connect to the ftp server
$conn_id = ftp_connect('ftp.example.com');
$login_result = ftp_login($conn_id, 'user', 'password');
//if the connection is ok, then...
if ($login_result) {
//iterate each zipped file
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
//create a "local" temp file in order to put it "remotely" in the same machine
$temp = tmpfile();
//create the new file with the same name from the extracted file in question
ftp_fput($conn_id, $ftp_path_to_unzip . $filename, $temp, FTP_ASCII);
//set write permissions, eventually we will put its content
ftp_site($conn_id, "CHMOD 0777 " . $ftp_path_to_unzip . $filename);
//open the new file that we have created
$fp = fopen($local_path_to_unzip . $filename, 'w');
//put the content from zipped file
$content = $zip->getFromName($filename);
fputs($fp, $content);
//close the file
fclose($fp);
//now only the owner can write the file
ftp_site($conn_id, "CHMOD 0644 " . $ftp_path_to_unzip . $filename);
}
}
// close the connection and the file handler
ftp_close($conn_id);
//close the zip file
$zip->close();
}
This is the firt step to start a more complex customization, because the code above is not able to know if the zipped file is a "directory" or "file".
I am trying to read a video file uploaded on server using fopen and fread in php but fopen returns "unable to open file".
//test.php
<?php
$file=fopen("abc.mov","r") or exit("Unable to open file!");
?>
abc.mov exists in the same folder where test.php is located on the server i.e, at the same hierarchy.
I don't why it isn't able to read the file.
Please help.
This probably isn't a real problem with PHP or your file. This is most likely a problem with the permissions of the file. There are three things you can try here(probably more I don't know of). One, do this somewhere before the fopen in your script:
chmod("abc.mov", 0777);
Then echo fileperms(), just to check(take out after debug):
echo fileperms("abc.mov");
And lastly, before calling fopen, make sure that is_readable and file_exists return true:
if(file_exists("abc.mov") and is_readable("abc.mov")) {
$file = fopen("abc.mov","r") or exit("Unable to open file!");
}
else die("File isn't readable, or maybe doesn't even exist!");
Note: I would be using file_get_contents() and file_put_contents() rather than fopen.
Hope this helps!
Check your file access permissions to make sure you've got access to the file from PHP.