file_exists ?

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!

Moderator: General Moderators

mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

file_exists ?

Post by mikegotnaild »

Hey. I got a problem with this code.

Code: Select all

$imagefile = $_FILES['imagefile']['name']; 
$mp3file = $_FILES['mp3file']['name'];

//Check to see weather there is a file in our upload folder with the same name
   if (file_exists($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/'.$imagefile)) {
		echo "There is already a file called <b>$imagefile</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT.";
		exit();
		}
		
     //Check to see weather there is a file in our upload folder with the same name   
	if (file_exists($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/'.$mp3file)) {
		echo "There is already a file called <b>$mp3file</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT.";
		exit();
		}
This obviously isnt the full script. Even if there isnt a file with the same name as the ones i want to upload it still gives me that error message

Code: Select all

echo "There is already a file called &lt;b&gt;$mp3file&lt;/b&gt; on our server. &lt;b&gt;Please rename this file.&lt;/b&gt; &lt;br&gt;&lt;br&gt;Click the Back button and RE-SUBMIT.";

?>
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

bump if u dont mind
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

hmm
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Wel, unless you've discovered a bug in file_exists() (which i doubt after the length of time it's been in use :o) then it must be either :
1. The file does exist
2. $mp3file isn't set
3. A symlink problem

I'd make sure you have error_reporting(E_ALL) at the top of the script, that should rule out number 2, number 1 is easy to debug .. just go look ;) Number 3 i wouldn't have thought would be a problem, but check that $_SERVER['DOCUMENT_ROOT'] is a 'real' path and not a symlink of some kind.
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

1. File Doesnt exist for sure.

well $_SERVER['DOCUMENT_ROOT'].'/localmm/upload/' is the same thing i use for uploading.. and the files uploaded correctly to that directory before i started this new file_exists check. i will do what you said.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just another thing to check, is there anything strange about the file name that are being uploaded? ie Do they have any special chars in them, like spaces etc.. ?
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

nope
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Hmm..are you wearing blue socks? File uploads only work if you are wearing blue socks.</running out of ideas> :o
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

i put error_reporting(E_ALL); in my script and tried it but the only out put was that error message i made for the check

Code: Select all

echo "There is already a file called <b>$mp3file</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT.";
\

Would u like my full script? Oh and $mp3file doesnt out put anything when that error shows up.. It just says

There is already a file called on our server. please rename this file

Click Back button and RE-SUBMIT.
Last edited by mikegotnaild on Tue Mar 02, 2004 9:18 pm, edited 2 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Would u like my full script?
Sure.
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

Code: Select all

<?

error_reporting(E_ALL);
//MySQL Variables
$host = "host";
$login_name = "login";
$password = "pass";

//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");

//Select the database
MySQL_select_db("bandsandmembers") or die("Could not select database");

//Assign contents of form to variables
$bandname = $_POST['band_name'];
$description = $_POST['description'];
$history = $_POST['history'];
$influences = $_POST['influences'];
$genra = $_POST['genra'];
$email = $_POST['email'];
$website = $_POST['website'];
$imagefile = $_FILES['imagefile']['name']; 
$mp3file = $_FILES['mp3file']['name'];
$types = array("image/gif","image/jpeg","image/bmp","image/pjpeg","image/x-windows-bmp");
$types2 = array("audio/mpeg","audio/mp3","audio/x-mpeg-3","audio/x-mp3","audio/m3u","audio/x-m3u","application/x-compressed","application/zip","multipart/x-zip","application/x-troff-msvideo","video/msvideo","video/x-msvideo","application/x-compressed","video/avi","video/mpeg","audio/mpeg3","video/mpeg","video/x-mpeg");


    
   //This is the File upload part
   $uploaddir = $_SERVER['DOCUMENT_ROOT'].'/localmm/upload/';
   $uploadfile1 = $uploaddir . $_FILES['imagefile']['name']; 
   $uploadfile2 = $uploaddir . $_FILES['mp3file']['name'];
   
   
   //Check to see weather there is a file in our upload folder with the same name
  if(file_exists($uploaddir . $imagefile)) { 
echo "There is already a file called <b>$imagefile</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT."; 
$uploaded = glob($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/*.*'); 
foreach($uploaded as $up){ 
    if($up == $imagefile){ 
        echo '<b>'.$up.'</b><br />'; 
    } else { 
        echo $up.'<br />'; 
    } 
}    
exit(); 
}
		
     //Check to see weather there is a file in our upload folder with the same name   
	if(file_exists($uploaddir . $mp3file)) { 
echo "There is already a file called <b>$mp3file</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT."; 
$uploaded = glob($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/*.*'); 
foreach($uploaded as $up){ 
    if($up == $mp3file){ 
        echo '<b>'.$up.'</b><br />'; 
    } else { 
        echo $up.'<br />'; 
    } 
}    
exit(); 
}
   
   if (in_array($_FILES['imagefile']['type'], $types)){
   move_uploaded_file($_FILES['imagefile']['tmp_name'], $uploadfile1);
       }else{
	   echo "There was an error when uploading. Your file could have been too big. or Check to see if you uploaded the correct file type. Allowd file types are. .gif, .jpg, .jpeg, .bmp";
}
   if (in_array($_FILES['mp3file']['type'], $types2)){ 
   move_uploaded_file($_FILES['mp3file']['tmp_name'], $uploadfile2);
   }else{
	   echo "There was an error when uploading. Your file could have been too big. or Check to see if you uploaded the correct file type. Allowd file types are. .mp3, .avi, .mpg, .mpeg, .zip";
}    
   

$sql = "INSERT INTO $genra (band_name, description, history, influences, genra, email, website, imagefile, mp3file) VALUES ('$bandname','$description','$history','$influences','$genra','$email','$website','$imagefile','$mp3file')";



$result = mysql_query($sql);

//Code to check if statement executed properly and display message
if ($result) {

} else {
echo("An error has occured");
}
//Close connection with MySQL
MySQL_close()
?>
Last edited by mikegotnaild on Tue Mar 02, 2004 9:38 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

hmm..all looks ok. Don't know if this will help, but you could try this extra debugging.

Code: Select all

if (file_exists($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/'.$mp3file)) { 
echo "There is already a file called <b>$mp3file</b> on our server. <b>Please rename this file.</b> <br><br>Click the Back button and RE-SUBMIT.";
$uploaded = glob($_SERVER['DOCUMENT_ROOT'].'/localmm/upload/*.*');
foreach($uploaded as $up){
    if($up == $mp3file){
        echo '<b>'.$up.'</b><br />';
    } else {
        echo $up.'<br />';
    }
}    
exit(); 
}
This should just list the files already in the upload dir and highlight any matching ones in bold. Shouldn't show any suprises but it's worth a go :o
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

what happens when u try using

if(file_exists($uploaddir . $imagefile)){

??
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

LiLpunkSkateR wrote:what happens when u try using

if(file_exists($uploaddir . $imagefile)){

??
I had thought about doing that but someone gave me the code snippet im using right now and since $uploaddir 's value means the same as what i have down i didnt think it would matter. But ill try the extra debugging and if(file_exists($uploaddir . $imagefile)){

ILL...BE.. BACK
mikegotnaild
Forum Contributor
Posts: 173
Joined: Sat Feb 14, 2004 5:59 pm

Post by mikegotnaild »

still doesnt work. *shakes head*

I shall edit my post with full code to the new code u guys gave me. just for the heck of it

There is already a file called $imagefile on our server. Please rename this file.
and i get:
"There is already a file called on our server. Please rename this file."

So this means $imagefile isnt putting out anything... Any ideas?
Last edited by mikegotnaild on Tue Mar 02, 2004 9:40 pm, edited 1 time in total.
Post Reply