help me to solve the problem .
I have wrote my code like this :
function copy(){
$folder = "PDF";
$file = "2016-01-001_01-00.pdf";
$files = "ftp://10.242.42.154/adk_rkakl2016s"." ".substr($file,8,3)."/".substr($file,12,2)."/".$folder."/"."2016-01-001_01-00.pdf";
$newfile = 'files/backup_file/2017-01-001_01-00.pdf';
if (!copy($files, $newfile)) {
echo "failed to copy $files...\n";
}else {
echo "copied $file into $newfile\n";
}
}
i want to copy file from another server, but the result like this :
Severity: Warning Message: copy(): connect() failed: Connection refused Filename: controllers/Download_adk.php Line Number: 146
please help me
Since it is saying connection refused,
Please check whether you are able to view the PDF in browser.
If you can view pdf that is on the same machine as your program, then it might be that the server won't send the pdf unless you are a real user. In that case, modifying the browser identification string might fix your problem.
If you cannot view the pdf from your browser, then I guess, we have to check for some other problem.
Related
As a part of some automation scripts I'm writing a function that asks the user for an absolute path to the file she wishes to use. The problem is that PHP throws Warning: fopen("/path/to/desired/file/file.txt"): failed to open stream: No such file or directory in /path/to/my/script/script.php on line 13 when the user inserts the file. Here is the general function:
function askFile() {
echo "Please specify the full path to your file.\n";
$usrInput = trim(fgets(STDIN, 1024));
$usrInput = '"' . $usrInput . '"';
echo $usrInput . "\n"; // Just to test the final string.
echo is_string($usrInput) ? "It's a string.\n" : "It's not a string.\n"; // Just to confirm that the variable holds a string.
if($handle = fopen($usrInput, 'r')) {
echo "Thank you. The file is now opened.\n";
} else {
"Sorry, could not open the file for reading.\n";
}
}
askFile();
The permissions for the file are, for testing purposes, wide open at 777. When I simply hard code the absolute path right inside fopen() everything works as expected, therefore the problem only occurs when the input comes from STDIN.
We're making a Foodlist site which has an Admin that has the privilege to "Add Food" and "Edit Food" which includes giving images. But all trials to send the food image to the actual computer we're going to use as a server has failed. It'd be weird to copy the image everytime we edit something or add something.
Our site requires us to simply send a file from our computer to another in their "xampp\htdocs\sitepage\images" folder. We're on the same network. Here's our code.
$my_file = 'C:\Users\ASUS\DownloadsMyFile.jpg';
/* New file name and path for this file */
$destination_file = '192.168.1.105/sitepage/images/INeedSomeRest.jpg';
/* Copy the file from source to server */
$copy = copy( $my_file, $destination_file );
/* Add notice for success/failure */
if( !$copy ) {
echo "It didn't work.";
}
else{
echo "WOOT! Successfully copied the file.";
}
It always gives us an error such that the browser says something like:
" failed to open stream: No such file or directory in ~our php file~ "
Is there something we're doing wrong? That being said, is there another way to send a file to another computer via PHP?
A Response will be greatly appreciated. Thank you in advance.
It seems I took the "copy" method of php all wrong. it seems it can only take a file from another server to your computer, and not the other way around. To the people who have the same problem as me, try to read about File Transfer Protocols. =) I appreciate all the replies. Thank you.
1) Try to use $destination_file = 'http://192.168.1.105/sitepage/images/INeedSomeRest.jpg'; (note about http://)
2) Make sure that you CAN write to that resource (enough rights, shared properly, etc)
This question already has answers here:
failed to open stream: HTTP wrapper does not support writeable connections
(4 answers)
Closed 7 years ago.
SO I am getting this error and searched everywhere for a reason. From what I have read this code should work. It is not using a url to write the file but instead a relative path. All the file permissions are set up right to 755 so dont think that is the issue. I even had a plugin developer looked at it and he didnt see an issue. Anyone got any ideas why this error is happening?
Error:
`Warning: move_uploaded_file(http://************/wp-content/uploads/simpleecommcart/digitalproduct/13-1.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/***********/public_html/wp-content/plugins/simple-e-commerce-shopping-cart/models/SimpleEcommCartProduct.php on line 880`
`Warning: move_uploaded_file(): Unable to move '/tmp/phpyv2ztG' to 'http://***********/wp-content/uploads/simpleecommcart/digitalproduct/13-1.jpg' in /home/stagingpeak/public_html/wp-content/plugins/simple-e-commerce-shopping-cart/models/SimpleEcommCartProduct.php on line 880`
The upload code (I wrote 880 where the error was):
//check for product image upload
if(strlen($_FILES['product']['tmp_name']['image_upload']) > 2) {
$dir = SimpleEcommCartSetting::getValue('product_folder');
if($dir) {
$filename = preg_replace('/\s/', '_', $_FILES['product']['name']['image_upload']);
$path = $dir . DIRECTORY_SEPARATOR . $filename;
$src = $_FILES['product']['tmp_name']['image_upload'];
880--> if(move_uploaded_file($src, $path)) {
$_POST['product']['product_image_path'] = $filename;
}
else {
$this->addError('Product Image File Upload', __("Unable to upload file","simpleecommcart"));
$msg = "Could not upload file from $src to $path\n". print_r($_FILES, true);
throw new SimpleEcommCartException($msg, 66101);
}
}
}
The HTTP wrapper doesn't allow writing (for good reason) so this won't work in the way you were hoping, you'll need a stream that allows writing such as:
(Preferably) File: http://php.net/manual/en/wrappers.file.php
FTP: http://php.net/manual/en/wrappers.ftp.php
Source: http://php.net/manual/en/wrappers.http.php
I was following a guide for creating a new file in php and I was using the following code:
function create($user, $name) {
/* Later on, this will connect to another server*/
$dir = $user->getFolder() . "/Projects/". $name;
if(file_exists($dir)) {
$this->error = "Directory: " . $dir . " already exists.";
} else {
mkdir($dir);
//Create the users.json file and add the owner
$json = fopen($dir . "/Data/users.txt", "w") or die("Cannot open file");
fclose($json);
}
}
The directory gets created but I receive the following error: "Warning: fopen(Jake/UserFolder//Projects/test/Data/users.txt): failed to open stream: No such file or directory in D:\xampp\htdocs\Collabs\Objects\Scripts\Project.php on line 14
Cannot open file"
The path shown in your error looks like it could be the problem.
Jake/UserFolder//Projects/test/Data/users.txt
There are two slashes between UserFolder and Projects. It looks like changing your code to
$dir = $user->getFolder() . "Projects/". $name;
Would get rid of the extra slash.
A couple of things that might have gone wrong:
You didn't pass path with a leading /, so fopen() will search relatively to the directory the script is executing in. I assume you'll want to pass an absolute path
you are testing for file_exists(), you should also check for is_dir(), to catch and handle the cases where a file with the same path already exists
you might consider calling mkdir($dir, 077, true) in order to create
the intermediary directories (see the mkdir() documentation)
I am trying to open a file for reading in php script but having trouble.
This is my code
$fileHandle = fopen("1234_main.csv", "r")or die("Unable to open");
if (!file_exists($fileHandle))
{
echo "Cannot find file.";
}
The script is in the same directory as the file I am trying to read and there are no other read/write permission errors as I can create/read other files in the same directory.
When I run the script I just get the "Cannot find file" error message. Why is this error message being shown? Surely if fopen() can't open the file the "or die statement" should end the script?
Also, why can't I open the file when it definitely exists and is in the same location as the script (I have also tried using the full path of the filename instead of just the filename).
I am fairly new to php (but have exp in c++) so if its a stupid question I apologize.
Many thanks
In PHP, file_exists() expects a file name rather than a handle. Try this:
$fileName = "1234_main.csv";
if (!file_exists($fileName))
{
echo "Cannot find file.";
} else {
$fileHandle = fopen($fileName, "r")or die("Unable to open");
}
Also keep in mind that filenames have to be specified relative to the originally requested php-script when executing scripts on a web server.
You can use file_get_content() for this operation. On failure, file_get_contents() will return FALSE.For example
$file = file_get_contents('1234_main.csv');
if( $file === false ){
echo "Cannot find file.";
}
file_exists() take the file-name as input, but the logic of your code has problem. You first try to open a file then you check its existence?
You first should check its existence by file_exists("1234_main.csv") and if it exists try to open it.
file_exists takes a string, not a file handle. See http://php.net/manual/en/function.file-exists.php