Dreamhost site doesnot support fopen functionality - php

Actually i want to send zip file to the dropbox .But when i try to open my file using fopen then this issue comes.
fopen(www.cloud01.wptemplate.net_09_10_2015_16_1444437876.zip): failed to open stream: No such file or directory in /home/cle1296/cloud01.wptemplate.net/wp-content/plugins/wp-cloud-safe/includes/UltimateBackup.php on line 830.
My server is dreamhost.I execute the same code on another server and don't face any issue.It seems that dreamhost disabled the fopen function.So kindly give me an alternative way
function sendToDropbox() {
try {
$this->log('Sending file to DropBox');
$dbxClient = new dbx\Client($this->dropboxGeneratedAccessToken, "PHP-Example/1.0");
$f = fopen($this->backupFilename, "r+");
$dbxClient->uploadFile($this->dropboxUploadPath . $this->backupFilename, dbx\WriteMode::add(), $f);
} catch (Exception $e) {
$this->log('ERROR while uploading file to DropBox');
}
}

The path to the file obviously is not correct, either because the file does not exist or simply what you passed to fopen() is not the actual location of the backup. Use the full path to the file to be absolutely sure, for example if your backup is at:
/home/cle1296/cloud01.wptemplate.net/my_backups/backup.zip
...then make sure to pass it to your fopen() and you shouldn't have any issues.

Related

How to open a ZIP using remote url?

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

PHP file_put_contents works with file, no file gets "failed to open stream: No such file or directory"

The PHP file_put_contents function works perfectly fine when the file exists. If the file does not exist I receive the error "failed to open stream: No such file or directory".
$file = '../templates/stuff.xml';
if (!file_exists($file)) {$file = '../'.$file;}
$var['xhtml'] = $_POST['post_xhtml'];
$file_contents = serialize($var);
file_put_contents($file,$file_contents);
I tried the same thing with fopen and fwrite using the correct flags (w, w+ and tried the others) yet still had the same problem: if the file already existed it worked just fine, otherwise it would give me the same error message.
I know the file path is correct. I'm using Windows 7 for local development.
When the file doesn't exist, you are prepending ../ to the path, thus you are trying to write to:
../../templates/stuff.xml
Are you sure that the folder ../../templates exists (and that PHP can write to it)?
Before you write to a file, you need to check that the folder exists. Try using is_dir():
if(is_dir(dirname($file))){
file_put_contents($file, $file_contents);
}

Linux Box using PHP writing file to Windows Server Web Share

We have a bunch of linuix and windows servers.
On my windows desktop I can see all the shares.
Using PHP I'm attempting to write a file to a directory on a windows share using the UNC path.
//ServerShare/directory/file.txt
Using fwrite says it successfully wrote the file but the file never exists on the server.
Using opendir function says the directory isn't accessible.
This is the source pretty simple.
$file_name = "\\SERVER2\Share\CPI\data.txt";
if (!$handle = fopen($file_name, 'w')) {
echo "Cannot open file ($file_name)";
exit;
}
// Write $somecontent to our opened file.
$somecontent = "this is a test";
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($file_name)";
fclose($handle);
Any thoughts on what types of permissions need to be set to let a linux box write files to a windows box?
You should be mounting the fileshare to a local directory:
mount -f smbfs //user#server2/Share/CPI/Data.txt /media/share
Then access /media/share/Share/CPI/Data.txt from your PHP script.
PHP needs to authenticate to the share, even if it is public, and using fopen or opendir does not do this.

Unable to open remote XML file using fopen(). What all permission i need to set in server or what parameters i need to set in fopen() function.?

I am trying to open an XML file in remote server through fopen() function.
I have two remote servers and the file permission of the xml is set to 777 in both servers.
i am able to open the xml from one server , but not from other. Both files can be opened in browser.
What all permission i need to set or what parameters i need to set in fopen() function.?
this is the function
function getFileData($ProjectName)
{
$file = fopen($ProjectName, "r") or exit("Unable to open File! ".$ProjectName);
$fileContent="";
while(!feof($file))
{
$fileContent.=fgets($file);
}
fclose($file);
echo $fileContent;
}
getFileData('http://serverA.com/myxml.xml'); // gives the content
getFileData('http://serverB.com/myxml.xml'); // gives the error fopen(http:/serverB.com/myxml.xml) [function.fopen]: failed to open stream: HTTP request failed!
You only have one / after http: in the last two lines - but I doubt that's the problem. Can you open the file in the browser?
Perhaps try using file_get_contents . It does the same as your function, except it's ready made and fully tested by the PHP guys.
I think there should be http://server and not http:/server.
Apart from the visible problem with URL, also check what is the value for allow_url_fopen set in your php.ini, if you can open file from one server but not from other server this may be correctly set for you, did you check about timeouts?

fopen unable to open a video file located on server

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.

Categories