is_file() / file_exists error with wordpress - php

I am having problems with both is_file() and file_exists(), i am trying to verify a featured image exists in the wp uploads folder....both output sets of characters and create multiple errors for each thumbnail size
fopen(http://www.xxx.gf/wp-content/uploads/2014/09/1-150x150.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/tserv/public_html/wp-includes/class-wp-image-editor.php on line 396
This is odd because i am passing a correct server path to both functions and the functions works for files outside the uploads folder. Also the filename is 1.jpg and its appending - 150x150 and a few other sizes which are the thumbnail sizes for wordpress.
update: server path is being passed: e.g. home/user/etc.., the error is saying fopen... but im not calling fopen directly nor passing a url to file_exists, is_file.
Since starting this i realize i'm mad for trying to verify it like this when i can check it using wp_functions but i would like to figure out why this is happening!
php code
if($x= is_file($upload_path.$filename) )
server- linux debian wheezy vps
open_basedir= None
safemode- off
folder permissions - 777 (temp)

You can't open the file using fopen using http and expect to write to the file. You will want to use fopen and specify the second option as "r" for read only or you can keep using this code but actually access it using the servers path and not http:// . You can also use file get contents.
try using http://codex.wordpress.org/Function_Reference/wp_upload_dir when accessing the upload
$upload_dir = wp_upload_dir(); // Array of key => value pairs
//base dir
$base_dir = $upload_dir['basedir'];

Related

php file_put_contents not creating file from url

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'));

Why is my PHP copy() function creating a blank file?

I have a copy function per say:
<?php
copy ( "$source" , "$destination" );
?>
The code does create a copy succssfully however the $destination (or copied file) is left blank. if i open it it is empty unlike the $source file.
The file size is 0b.
My situation:
nginx/1.10.0 (Ubuntu)
PHP 7.0.13-0
ubuntu0.16.04.1
The file permissions for the code processing file is 0777
The permission for the source file is 0777
The file permission for the destination file after being copyed (the blank one ) : 0644.
Obviously no control over the permission before copying.
Anyone know the reason for this and how to make the php copy() function copy the file with the original file contents?
Best regards,
AT
Ok so i seemed to solve this apparently my server ran out of disk space.
Makes sense. should have known.

php move_uploaded_file wont move the file to the hosted server

I don't know how to fix this but the problem is that it won't upload to the server that I'm using to host the website it creates the folder just fine but won't move it. The coding for the moving is below.
This is the error I get
Warning: move_uploaded_file(./userdata/profile_pics/iOy1pQXTZsLw7VA/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/a4640336/public_html/account_settings.php on line 103
and as well as this error code
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpX1zVno' to './userdata/profile_pics/iOy1pQXTZsLw7VA/' in /home/a4640336/public_html/account_settings.php on line 103
move_uploaded_file(#$_FILES["profilepic"]["tmp_name"],"./userdata/profile_pics/$rand_dir_name/".$FILES["profilepic"]["name"]);
echo "Your profile pic has been updated!".#$_FILES ["profilepic"]["name"];
//$profile_pic_name = #$_FILES["profilepic"] ["name"];
//$profile_pic_query= mysql_query("UPDATE users SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$username'");
//header("location: account_settings.php");
Overall I have tried to change where it is located to have it leading directly from the source but it doesn't change. If anyone can help please help me!
PS the commented out parts were done to be able to see the error
For those using PHP on Windows and IIS, you SHOULD set the "upload_tmp_dir" value in php.ini to some directory around where your websites directory is, create that directory, and then set the same permissions on it that you have set for your websites directory. Otherwise, when you upload a file and it goes into C:\WINDOWS\Temp, then you move it to your website directory, its permissions will NOT be set correctly.And If you try to upload a file larger than the post_max_size value (or multi files), the page will only refresh itself and no errors are thrown.
The destination directory must exist; move_uploaded_file() will not automatically create it for you.
You must
make sure that the file is not empty.
make sure the file name in English characters, numbers and (_-.) symbols, For more protection.
make sure that the file name not bigger than 250 characters.
Check File extensions and Mime Types that you want to allow in your
project. You can use : pathinfo(). or you can use regular expression for check File extensions as in example
Check file size and make sure the limit of php.ini to upload files
is what you want, You can start from here.
Check the file content if have a bad codes or something like this
function
move_uploaded_file($_FILES["file"]["tmp_name"], "../uploads/" . $_FILES["file"]["name"]);
Also check dir have writable permission
you need to use server path for file upload
$files = glob($_SERVER["DOCUMENT_ROOT"]."/myFolder/*");
$_SERVER["DOCUMENT_ROOT"] will get server path like var/host/public_html/your_folder
May this help you

Changing PHP Temp Directory

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.

php uploading file?

I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck

Categories