I'm working with two different APIs trying to transfer documents from one services to another. So far, the API has been kind of clunky to work with, but I was getting it to work, until now with the documents problem. Here is what I have to do:
1. I need to get the url of the document from the one service (which I have done).
2. Download it to my own servers to a folder called filesToUpload (which I have created).
3. Transfer it to the other services (which I have done with a document already on my servers.)
So as you can see, the problem isn't transferring it to the other service, the real problem is downloading it to my own servers. Here is what I have tried:(the $doc['url'] is the response from the one server, and I have confirmed that the url supplied is an existent document)
$fileToPost = str_replace(" ", "+", $doc["url"]);
$fileName = strchr($fileToPost, "-");
$fileName = substr($fileName, 1);
file_put_contents("/api/ricochet-clean/filesToUpload/$fileName", fopen($fileToPost, 'r'));
Here is my error message:
file_put_contents(/api/ricochet-clean/filesToUpload/uploaded-leads.s3.amazonaws.com/219-2018-06-10-10-19-04-Assign6.docx): failed to open stream: No such file or directory in <b>/Applications/XAMPP/xamppfiles/htdocs/SoftwareAPIs/api/ricochet-clean/index.php</b> on line <b>191</b><br />
In the file_put_contents() function call, I have tried the /api/ricochet-clean/filesToUpload/$fileName absolute path and I have also tried a relative path of filesToUpload/$fileName.
I saw this question: file_put_contents: Failed to open stream, no such file or directory
The answer to this was the developer hadn't created the directory he was trying to download the file to, that isn't my problem because I've confirmed I created the directory AND it is spelled the same both places.
What am I missing here?
The file you wish to write is
/api/ricochet-clean/filesToUpload/$fileName
but the file being written is
/api/ricochet-clean/filesToUpload/uploaded-leads.s3.amazonaws.com/219-2018-06-10-10-19-04-Assign6.docx
Assuming the directory uploaded-leads.s3.amazonaws.com does not exist, you will need to create it, or write code to create it.
The quotation marks you're using here are absolutely fine.
Will you try like this?
file_put_contents("'/api/ricochet-clean/filesToUpload/$fileName'", fopen($fileToPost, 'r'));
Related
I am developing a web application using laravel, and the application requires me to create a pdf file and save it to be downloaded later.I have successfully created the file from view using laravel-snappy and i can download it on the browser.The problem is that; when i try to save the file in a network shared drive using the file_put_contents function, I get the error captioned on the question's title.
When I change the path from network drive, to let say disk D:/test of the server, the file is successfully saved in that location.
I have tried the following ways to achieve it as below
$saving = "Z:/test";
Z is the network drive (drive of th shared folder)
1.By using barry laravel snappy facade
$pdf = PDF::loadView('file',compact('data'));
When I return it as below it works
return $pdf->download($fileName);//download the file
But when saving it in the path it gives me the error I mentioned above
$pdf->save($savePath);//saving the file
Using the file_put_contents function
$output = $pdf->output();
file_put_contents($savePath,$output);
Still getting the same error
3.Using the copy function
In the same drive/disk but different folders it works
$path1 = "D:/files";
$path2 = "D:/test";
copy($path1,$path2);
But when i change $path2 to the shared network drive/folder
$path1 = "D:/files";
$path2 = "Z:/test";
copy($path1,$path2);
It does not work and I get a different error as shown below
copy(Z:/test/39361_1657094148_elicense.pdf): failed to open stream: No such file or directory
When i change from Z:/test to IP of the shared folder as \\x.x.x.x\test it gives me another error as copy(\\x.x.x.x\test/39361_1657094148_elicense.pdf): failed to open stream: Invalid argument
I have tried this for 3 days now but nothing is working When I save to that shared folder/drive
I really need help. Anyone with an insight on how I can get out of this problem I will appreciate
Thanks in Advance
the issue was with the permission, my application had no right to write to that network shared folder
The solution was to open the directory and then save the file
I find the solution from this link https://www.codedwell.com/post/21/reading-file-list-from-a-mapped-windows-network-drive
Thanks for the insight #ADyson
I know that there are other questions similar to this, but I couldn't find the solution in it.
I am using absolute local pdf file and I want to use file_get_contents() function. My code:
$fileName = 'C:/Users/my_user/Desktop/thisispdf.pdf';
$content = file_get_contents($fileName);
The thing is that it says that the file is not found. When I put it the filename into file explorer, the pdf opens up immediately. I also was trying to change the slashes from / to \ and even use \\, but none of these helped.
What I am missing here?
It's possible the web server doesn't have permissions to access that user's files. If you move the file to an accessible or shared location it may work. (You can also change permissions to permit access, however doing that on the user's personal directory is not advisable since it could expose other information to unauthorized users.)
On different OSs, your path needs to be changed. Windows uses back slash. Try this
C:\\Users\\my_user\\Desktop\\thisispdf.pdf
Difference between forward slash (/) and backslash (\) in file path
I'm using two computers (both connected to one network) and one of them has XAMPP. I'm trying to upload files to the one with XAMPP in it (the files are from the other computer). But I always end up having the 'No such file or directory' error even though I have the correct path. But when I use the path from the computer with XAMPP, even when I'm using the other computer, the system works just fine. Can anyone help me?
P.S. I'm using PHP copy() function because the file path is coming from an excel file.
Here's the part of my PHP code:
$original_file_name = objWorksheet->getCellByColumnAndRow(5,$i)->getValue();
// Example of the cell value: C:\Users\ComputerWithoutXAMPP\Desktop\scanned documents\SO 2010\#1.jpeg
$ext = pathinfo($original_file_name, PATHINFO_EXTENSION);
$file = time().substr(md5(microtime()),rand(0,26),5);
// UPLOAD THE FILE DECLARED IN EXCEL
copy($original_file_name, 'uploads/docs/'.$file.'.'.$ext);
You can use copy() to upload a file to another machine, or from another machine, but to access the remote machine, the appropriate argument to copy (source or dest) has to be a URL. The code you've posted is trying to copy the file to a local "uploads/docs/" directory, it's doesn't even appear to aware of another machine.
While what you're looking to do may be technically possible, I haven't the foggiest idea how you'd go about it: it seems rather Rube Goldberg to me. The ftp:// wrapper would probably work, if FTP is set up properly on the XAMPP server.
How big is the file you're trying to send? If it's small enough, you may have better luck either encoding and sending the content itself or uploading the file with curl to an upload-catching script on the XAMPP side
I have a site for media conversion where users can upload video, audio or image files and it is converted in to 3 popular formats. Conversion script works fine but I am having some issues with the tmp directory where the files get uploaded to. I have tried 2 scenarios I am fine with either but neither works and it seems to me to be permissions related but I can seem to fix the problem.
I am working locally right now while I work out the kinks.
Scenario 1:
File is uploaded to default local tmp directory (C:\WINDOWS\tmp) - works fine
Attempt to run conversion script using tmp file that was uploaded and it doesn't work - run from command line works perfectly fine though
Scenario 2:
File is uploaded to directory I have given IIS_IUSRS full control of (for testing) and file won't upload - yes the directory is spelt correctly (I changed the upload_tmp_dir value in php.ini)
Both the site the javascript that send the XMLHttpRequest to the PHP file, as well as the site the PHP file itself reside on are IIS sites so I assume the script is being run as IIS_IUSRS.
EDIT: Temp file is no longer being created at all for Scenario 1, can't figure out why I am assuming playing with permission messed something up because the code hasn't changed. I've given Modify to IIS_USRS and USERS to try and get it working again but no luck :( although the error log is still writing to the same folder...weird
NOTE: The "tmp_name" value of the $_FILES variable I am sending still has a value of "C:\WINDOWS\Temp\'filename'" but the file is not there
EDIT: Another new development, it appears it is NOT a permissions issue because I can create a temp file via $temp_file = tempnam(sys_get_temp_dir(), 'Test'); however it obviously does not contain the uploaded data so it does not solve my problem
PHP is ignoring the upload_tmp_dir because of one setting on APPLICATION POOLS.
It's not php-cgi.exe nor php.ini or a permissions issue.
Go to the application pool of the website experiencing the issue:
1. right click
2. select advanced settings
3. scroll to LOAD USER PROFILE and set it to FALSE.
that did the trick for me.
This is less of a problem solved and more of a workaround. The issue seems to be something with the
$_FILES['file']['tmp_name'];
When I echo the contents it looks as I expect however no file appears. So rather than taking advantage of that process that happens naturally, I have had to take a manual approach. What I have done is the following:
create a temp file
$temp_file = tempnam(ini_get('upload_tmp_dir'), 'php');
then add the content from the temp file created during the $_POST request. (which some how are in the $_FILES variable even though the file is not when I look in the directory)
file_put_contents($temp_file, file_get_contents($_FILES['file']['tmp_name']));
the I have my temporary file for processing.
I'm trying to load data from a CSV on my Windows PC into a database, something I've successfully done previously. fopen can't find my input file.
Here's the specific code I'm having trouble with:
<?php
ini_set('track_errors', '1');
$handle = fopen("C:/Users/Sam/Documents/test.csv", 'r') or die("can't open file: $php_errormsg");
?>
The error printed is:
[function.fopen]: failed to open stream: No such file or directory
The file definitely exists, and I get the same problem on Unix machines. How do I fix this?
Windows 7 (and Vista?) only lets a user access his own home directory and does not allow Apache (or other users) to. Unfortunately, this is a major headache, and I would suggest that you just move the file somewhere public.
This type of behavior is easier to fix in Linux, but you're still better off moving the file out of your directory into some path where Apache has read access.