Trying to write file, file not being written - php

I am trying to write the contents of a variable to a .php file so that I can access that file later, through requite_once, and use that variable later.
The dir that I am trying to write to has 755 permission and I talked to my hosting provider who said that the dir can be written to by me now.
But, the file is not being written. Here is the code that I am trying to write the file with:
error_reporting(-1);
$elasticaObject = var_export($elasticaObject, true);
file_put_contents('theIndex.php', $elasticaObject);
echo "done!";
I get an output of done!done!done!
but, the file is not being written as I get a 404 error when I try to visit it and because it is not-existent in the dir listing.

You must set the write executing permission on the file that contains the script.
This is a good guide:
http://www.phpjunkyard.com/tutorials/ftp-chmod-tutorial.php

Related

file_get_contents failure on windows

I have a PHP CLI application that creates a file with file_put_contents then listens for changes to that file. If the filemtime changes then I try to get the content with file_get_contents. It often fails to retrieve the contents on windows. This baffles me. How is it possible that a process that creates the file cannot open the file?
I even ran icacls on the folder that the file is in and it still fails to have access to read the file that it created.
icacls.exe 'MYFOLDER' /grant MYUSER:(OI)(CI)F /T
Can someone please enlighten me on how to insure a PHP process can read a file it created?

php unlink returns file not found

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.

Copying via PHP results in URL mismatch

I have a code which would copy a file from one location to another.When I try to access the copied file,I am receiving file not found error,but the file is actually present in that location.
Location of source file:codebase\assets\default_pages\
Location of file to be copied:codebase\site_users\general\test\
Source file:home.php
Destination file:test.php[Rename while copying]
Code:Test.php[location:codebase\models\user_entry\]
copy("../../assets/default_pages/home.php","../../site_users/general/test/test.php");
When I try to access test.php,it gives me following error,
/codebase/site_users/general/ravikanth/codebase/site_users/general/test/test.php
But when I create a file manually[without code]everything works fine.Thanks in advance for your help.
i think you must be having some sort of redirect inside your php file.
can you paste the content of the file which your trying to access after copying
It could be a simple file permission problem. Try chmod command. Maybe manually you are creating/copying with root but Apache or whatever the PHP runs on does not have a permission.

File not created (Linux-PHP-C++)

I am trying to save a file to the current directory in Linux and then display it on a webpage. Currently I run a C++ executable from a php script with the following code
exec("/var/www/html/radsim/plotFluence $rMin $rMax $zMin $zMax $lum $graphStyle $basepath[$path]", $return);
When I run the executable from the console in Linux the file is created fine, the problem arises when I try from within the php; the file is simply not in the directory . The user inputs values and the executable is run but no file is made. The C++ looks like this
canvas->Print(("/var/www/html/radsim/"+histoName+_FileFormat).c_str());
The permisions are set to 777. In addition, on another PHP script, I use fopen("data.txt", 'w') or die() to create a text file, but it always dies.
Seems like a sandbox. There must be a php config option - best to start here: http://php.net/manual/en/configuration.changes.php

file uploaded via php: no such file or directory

i'm working on a website wherein the users can upload images (uses php 4.3.11). the files are uploaded with no problem as i can see them in the upload directory and i don't get any error message, but when i try to access the uploaded files via ftp, i get an error: no such file or directory. sometimes, i am able to access the file sometimes i get this error. what could be the problem here?
[update]
thanks for the help guys. i'm not familiar with the ftp daemon stuff. but i do access my files via ftp using FireFTP. the files are there but when try to download them or change the file properties, i get the said error. i also tried uploading a file in the folder through ftp and i was able to download it with no problem.
here is some of the code i'm working on, its kind of roundabout but i'll see on how to improve it.
my working directory is something like this www.domain.com/register/
and the upload directory is here www.domain.com/register/uploads/
users are required to register and upon sign-up, a folder is created for them in the uploads directory. i couldn't find a way to create a folder without having to be in the uploads folder itself so i redirect to a create-user-folder.php file in the uploads dir.
the file just contained this code:
$user_foldername = rawurldecode($_GET['name']);
mkdir($user_foldername);
header("Location: ../form.php"); // redirect back to the page
i checked and the created folder's permission is set to 775.
and here's part of the code i use in uploading ( /register/function/function.php ):
$path = "../uploads/$user_foldername/";
for($j = 0; $j < $num_of_uploads; $j++){
if(is_uploaded_file($_FILES[$file]['tmp_name'][$j])){
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
}
}
i checked using FireFTP and the files are in the /uploads/user_foldername/ directory and its permission is set to 664. the strange thing is that when i try to download the files, at times there would be no problem at all but there are times when the error will appear.
[another update]
i added chmod() after the copy() function,
$filename = $_FILES[$file]['name'][$j];
copy($_FILES[$file]['tmp_name'][$j],$path.$filename);
chmod($path.$filename, 0755);
but i still get the error.
another thing is that when i access /register/uploads/user_foldername/ through the url, i can see all of the uploaded files and view them, but how is it that i can't access them via ftp?
thanks again!
This is either a permission issue, or a configuration error. Here are things you should try:
What are the permission of the uploaded files? Does the FTP user has access to these files? Have you tried logging in as the user the FTP daemon would use and see if you could read the file that way?
Do you really see the correct directory? Have you verified by putting a file in that directory yourself and downloading it? Have you used the ftp command ls to verify the presence of the folder/folders/files?
You might need to chmod the folder the files are in, or in some cases the files themselves.
try chmoding them to 775
You can chmod files and folders through PHP it's self, with the chmod function. Or, you could use a FTP program such as filezilla.
Also check to make sure the intermediate directories are also permissioned as 755, as all the directories in the path need to be executable to be traversed.
i just figured out the problem. it was all because of the file name having accented characters in it, which explains why i do not always get the error message :|
<sigh> i should have seen this earlier, but anyway i hope this helps in case someone ran into the same problem.
thanks again! i really appreciate it :)

Categories