php unlink returns file not found - php

So, I have a script in
/var/www/vhosts/Domain/SubDomain/Script.php
that writes images to
/var/www/vhosts/Domain/wwwDomain/PhotoLocation/
and this works fine.
However, when I run this script:
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php
which uses the 'unlink' command
unlink ("/var/www/vhosts/Domain/wwwDomain/PhotoLocation/Image.jpg")
I get the error
"PHP Warning" "No such file or directory in
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php"
I thought that since I could WRITE the file fon a different subdomain, I could Delete from the other subdomain as well.
am I missing something?
Do I need to set Permissions somewhere else, or set a path differently? I specifically call the File by Absolute Path, and I can verify that the file exists there.

All looks fine. Some suggestions:
files are case sensitive
check if the folder Photolocation has write permission (it usually will for you can write to the location)
close the handle after you've created the file. If the handle is still open, delete is not possible
check with file_exists() just to make sure the file does exist
did you create the file or a shortcut?
when creating the file, do not suppress possible errors. It might be a header problem in the created file itself. Check your error-files for clues.

Related

PHP unlink for symlink pointing actually not existing file is not working

I have a strange issue with unlink.
when I use unlink() function, it removes the symlink which is linked to existing file correctly.
But for the symlink file which is actually pointing un-existing file, it does not work.
I have googled here and there, but can not find the right reason.
The working flow is like this:
first PHP file removes the source file (which is triggered by ajax request)
and then second php file tries to remove the symlink which is pointing the file just removed by first PHP file.
But second PHP file fails to remove the symlink.
Any idea why this kind of thing is happening?
Well, it is due to file_exists() function.
Before unlink the file, it checks to see if file is exists using file_exists() function.
But the problem is this function returns false if symlink is invalid.

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.

how to delete a file from a directory using php

From the title you can see i am looking for a way to delete a file from a different directory. All i can find on the subject is the unlink(), but from what i read in the documentation and from testing that function is it deletes the file name from the code you put it in. Makes me think it's quite similar to closing the file. What I am trying to do is actually delete a file using code so my user doesn't have to manually go to the folder and find the song they just deleted from the mysql database.
unlink() will delete the file off your server
rmdir() will delete a directory off your server
Note: Once it's gone, it's gone.
unlink truly deletes the specified file from the disk
We can delete files by giving its URL or path in PHP by using unlink command. This command will work only if write permission is given to the folder or file. Without this the delete command will fail. Here is the command to delete the file.
$path="images/all11.css";
if(unlink($path)) echo "Deleted file ";
realpath — Returns canonicalized absolute pathname
is_readable — Tells whether a file exists and is readable
unlink — Deletes a file
Run your filepath through realpath, then check if the returned path exists and if so, unlink it.

How do I make move_uploaded_files work in PHP?

What I have right now for file upload is:
move_uploaded_file($filetemp, "files/$filename");
With filetemp referring to $_FILES['fileupload']['tmp_name'], filename referring to $_FILES['fileupload']['name'], and files referring to a folder of that name inside the folder where the PHP file is.
However, this does not move the file to the files folder. How do I make it so that the function moves the file there?
Thanks!
Nerd With a Vengeance
There could be any number of reasons why this might not be working.
The first thing to check is permissions - make sure the webserver has write permissions to the directory you're trying to write to.
Also, turn your error reporting up - see what warnings are being generated on failure (assuming that you're return value is indeed false).

move_uploaded_file not working

I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.

Categories