Page 1 of 2

Array() error being generated from file_exists()

Posted: Mon Mar 22, 2004 9:11 am
by mjseaden
Dear All

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:

Code: Select all

if(!file_exists($_POST('photo1_upload')))      <-- this is line 2366
&#123;
    	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();
&#125;
Thanks for any help,

Mark

Posted: Mon Mar 22, 2004 9:15 am
by malcolmboston
im not sure if !file_exists is a valid function so try

Code: Select all

if
(file_exists($_POST('photo1_upload')))
print "file already exists";
}
else
{
print "file doesnt already exist";
}
also try running

Code: Select all

if (isset($_POST['photo1_upload']))
{
print "the variable is definitely there";
}
else
{
print "the variable hasnt been set";
}
this will make sure the variable is there for the script to use

Posted: Mon Mar 22, 2004 9:23 am
by mjseaden
Hi Malcolm,

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.

Could it be something to do with my PHP version?

Cheers

Mark

Posted: Mon Mar 22, 2004 9:28 am
by Wayne
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 ...

Code: Select all

if(!file_exists($_POST&#1111;'photo1_upload']&#1111;'name']))
or

Code: Select all

if(!file_exists($_POST&#1111;'photo1_upload']))

Posted: Mon Mar 22, 2004 9:28 am
by malcolmboston
PHP version shouldnt be a factor here

gimme like 50 lines or so of BEFORE line 2366 and ill have a look

it is very possible it is something from before that line

Posted: Mon Mar 22, 2004 9:31 am
by mjseaden
You got it:

Code: Select all

// uploadphotos()
// Uploads property photos to a temporary directory for viewing on the data entry form, for before submission
function uploadphotos()
&#123;
	// 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&#1111;'photo1_upload']) || empty($_POST&#1111;'photo1_upload']))
    &#123;
    	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();
    &#125;

    if(!isset($_POST&#1111;'photo2_upload']) || empty($_POST&#1111;'photo2_upload']))
    &#123;
    	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();
    &#125;

    if(!isset($_POST&#1111;'photo3_upload']) || empty($_POST&#1111;'photo3_upload']))
    &#123;
    	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();
    &#125;

    if(!isset($_POST&#1111;'photo4_upload']) || empty($_POST&#1111;'photo4_upload']))
    &#123;
    	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();
    &#125;

    // Do checks on local files to make sure filenames are valid and exist
    if(file_exists($_POST('photo1_upload')))
    &#123;
    &#125;
    else
    &#123;
    	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();
    &#125;
Hope that helps

Mark

Posted: Mon Mar 22, 2004 9:33 am
by Wayne
why did you swap from
$_POST['photo1_upload'] to $_POST('photo1_upload') ???

Posted: Mon Mar 22, 2004 9:33 am
by malcolmboston
maybe?

Code: Select all

if(!file_exists($_POST['photo1_upload']['name']))

Posted: Mon Mar 22, 2004 9:42 am
by mjseaden
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?

Thanks for the help.

Mark

Posted: Mon Mar 22, 2004 10:07 am
by mjseaden
/bump

Posted: Mon Mar 22, 2004 10:21 am
by Wayne
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']

Posted: Mon Mar 22, 2004 10:26 am
by mjseaden
Hi Wayne

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.

Any ideas what's causing this??

Posted: Mon Mar 22, 2004 10:28 am
by Wayne
are you uploading to a windows or *nux server?

Posted: Mon Mar 22, 2004 10:28 am
by mjseaden
Windows server

Posted: Mon Mar 22, 2004 10:30 am
by Wayne
this should work, change the code to this and let us know what happens.

Code: Select all

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(); 
    }