PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm trying to use the file_exists() function to check the existence of a directory/file stipulated by the user in a data form. However, when the function is executed on a pathname I have checked to be valid, I get the following error message:
Fatal error: Call to undefined function: array() in \\nas09ent\domains\s\XXXX\user\htdocs\XXXX.php on line 2366
I don't know why I'm getting an error generated by array(). Line 2366 contains the file_exists() function, acting on a $_POST var, like so:
if(!file_exists($_POST('photo1_upload'))) <-- this is line 2366
{
echo 'Cannot find file for photo 1. Please make sure you have entered the correct filename and path for photo 1. Click <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">here</FONT></A> to return to the data form.';
exit();
}
The script code previous to that bit of script does check that the variables are there. I've done what you said in your first response - I still seem to be getting the same error.
If this is an uploaded file then the variable is an array which will have several elemants ... the filename the tempfilename and filesize etc.
so you might need to try ...
// uploadphotos()
// Uploads property photos to a temporary directory for viewing on the data entry form, for before submission
function uploadphotos()
{
// Start session in order to set session variables equal to those posted, but only if
// the paths and files given pass validity checks
session_start();
do_html_header('Upload Photos', TRUE);
do_html_title('<STRONG>Upload Photos</STRONG>');
// First check that all upload files were stated before saving to session variables
if(!isset($_POSTї'photo1_upload']) || empty($_POSTї'photo1_upload']))
{
echo 'You have not stated a file for photo 1. Please go back to the <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">data form</FONT></A> and check that you have stated all required information.';
exit();
}
if(!isset($_POSTї'photo2_upload']) || empty($_POSTї'photo2_upload']))
{
echo 'You have not stated a file for photo 2. Please go back to the <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">data form</FONT></A> and check that you have stated all required information.';
exit();
}
if(!isset($_POSTї'photo3_upload']) || empty($_POSTї'photo3_upload']))
{
echo 'You have not stated a file for photo 3. Please go back to the <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">data form</FONT></A> and check that you have stated all required information.';
exit();
}
if(!isset($_POSTї'photo4_upload']) || empty($_POSTї'photo4_upload']))
{
echo 'You have not stated a file for photo 4. Please go back to the <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">data form</FONT></A> and check that you have stated all required information.';
exit();
}
// Do checks on local files to make sure filenames are valid and exist
if(file_exists($_POST('photo1_upload')))
{
}
else
{
echo 'Cannot locate file for photo 1. Please make sure you have entered the correct filename and path for photo 1. Click <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">here</FONT></A> to return to the data form.';
exit();
}
Thanks to you both. Maybe I should go have my eyes tested (the simple mistakes are always the hardest to spot...).
What's weird though is that I'm getting my custom error message coming up.
Cannot locate file for photo 1. Please make sure you have entered the correct filename and path for photo 1. Click here to return to the data form.
For the file 'D:\\BreakXL7.mp3'. This file exists, however is this an invalid path? Are the double backslashes \\ a problem, and if so should they be removed?
Or should I have file://...., in which case I would have to add them myself?
is that where the files are being stored? in D:\ ?? the \\ is correct if you are running your server on a windows platform. When checking the existance of the uploaded file you need to use the temporary filename of the uploaded file so use $_POST['photo1_upload'] or $_POST['photo1_upload']['tmp_name']
I'm using 'upload' and 'download' from the clientside perspective - i.e. the D:\ location is the location of the file on my harddrive, which I want to upload to a temporary directory on my site's FTP server. The FILE input objects on the dataform, from which the $_POST['photo1_upload'] variables are derived, are reporting that format of file path.
The files are valid, and they are there - and yet now file_exists is working it is saying that the file does not exist.
Is the double backslash \\ valid then? I know this is an old C/PHP trick when you wish to put a functional character is a string, however is that form correct for file_exists? I would have thought it would be.
if(file_exists($_POST['photo1_upload']['tmp_name']))
{
//then the file exists code here! place code here
}
else
{
echo 'Cannot locate file for photo 1. Please make sure you have entered the correct filename and path for photo 1. Click <A HREF="XXXX.php?p=22"><FONT COLOR="#FFFFFF">here</FONT></A> to return to the data form.';
exit();
}