Log in

View Full Version : PHP Copy Command Works with one glitch



jdsmith8
02-25-2015, 11:59 PM
Hi All,

I am using the PHP Copy call function to copy an existing file in a folder and rename it for the new page that is being created and it works to get the additional file created inside the folder, but then when I try to go to that file by way of direct url it gives a NO SUCH FILE OR DIRECTORY error.

Here is what I did to explain so someone can tell me where I went wrong:

1. I created a PHP file that has the Copy function to generate the desired file and that works fine and generates the file inside the newly created folder to hold it...


$listing_name = str_replace(" ","_",$listing_title);

$targetFile = "webpage/site_webpage.php";
$targetCopy="webpage/$listing_name.php";

copy($targetFile, $targetCopy);

if (file_exists($targetCopy)) {
$destination = "webpage/$listing_name.php";
} else {
echo "Failure: $targetCopy does not exist";
}

2. There is some code above that to get the $listing_name (title) to name the file, but all this works and that file is named site_detail_create_page.php and when ran generates the desired file and places it in the WEBPAGE folder that is in the main public_html directory with the site_detail_create_page.php file

3. But when I try to open that file through the web using www.mydomainname.com/webpage/$listing_name.php (whatever name assigned for file as is dynamic) it generates the NO SUCH FILE OR DIRECTORY error..

Does anyone know if I missed something like needing to declare that new folder of WEBPAGE somewhere so the server knows it exists, etc..?

Thanks in advance,
JD

jscheuer1
02-26-2015, 03:13 AM
Yes. You probably also need clearstatcache() which is specifically mentioned for use with file_exists() and a number of other stat functions:

http://php.net/manual/en/function.clearstatcache.php

before you check if the file you just created exists.

You should be able to use it without parameters:


$listing_name = str_replace(" ","_",$listing_title);

$targetFile = "webpage/site_webpage.php";
$targetCopy="webpage/$listing_name.php";

copy($targetFile, $targetCopy);
clearstatcache();
if (file_exists($targetCopy)) {
$destination = "webpage/$listing_name.php";
} else {
echo "Failure: $targetCopy does not exist";
}

But also should be able to specify the specific file. I'm not sure if it makes any difference. Read the man page to decide what might be best and experiment to see what works.



There could also be other problems.

Beverleyh
02-26-2015, 07:27 AM
I would also like to add that at the point where you check if the file exists, try checking with a full and absolute server path - something like;
if (file_exists('/home/www/mydomain.com/'.$targetCopy)) {