fopen() Problem

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

Post Reply
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

fopen() Problem

Post by kdidymus »

Folks.

This is driving me mad. I think it's a formatting error but I've been round in circles trying every combination I can think of. Basically, this section of my PHP interrogates a table in my dB and deletes all files listed under a particular code.

Code: Select all

$query2 = "SELECT filename FROM photos WHERE code='$code'";
    $result = mysql_query($query2)
    or die (mysql_error());

// SET THE LOOP TO DELETE PHOTOGRAPHS

while($row = mysql_fetch_assoc($result))
{
$filename2 = $query2;
extract($row);
$myFile = "../photos/$filename2";
$fh = fopen($myfile,'w') or die ("Can't open file");
fclose($fh);
unlink($myFile);}
And this is the error I keep getting...
Warning: fopen() [function.fopen]: Filename cannot be empty in /home/beautyby/public_html/admin/price.php on line 41
Can't open file
What am I doing wrong?!
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: fopen() Problem

Post by mikosiko »

kdidymus wrote:
$query2 = "SELECT filename FROM photos WHERE code='$code'";
    $result = mysql_query($query2)
    or die (mysql_error());

// SET THE LOOP TO DELETE PHOTOGRAPHS

while($row = mysql_fetch_assoc($result))
{
$filename2 = $query2;
extract($row);
$query2 is a resource... you can't use it in that way.

this should work ...

Code: Select all

$query2 = "SELECT filename FROM photos WHERE code='$code'";
$result = mysql_query($query2) or die (mysql_error());

// SET THE LOOP TO DELETE PHOTOGRAPHS

while($row = mysql_fetch_assoc($result))
{
   $filename2 = $row['filename'];
   $myFile = "../photos/$filename2";
   $fh = fopen($myfile,'w') or die ("Can't open file");
   fclose($fh);
   unlink($myFile);
}
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: fopen() Problem

Post by kdidymus »

Okay. I tried that. I replaced my old code with the code you provided...

Code: Select all

if ($delete == "yes")
{$query = "DELETE FROM prices WHERE code='$code'";
    $result = mysql_query($query)
    or die ("Couldn't execute query.");

$query2 = "SELECT filename FROM photos WHERE code='$code'";
$result = mysql_query($query2) or die (mysql_error());

// SET THE LOOP TO DELETE PHOTOGRAPHS

while($row = mysql_fetch_assoc($result))
{
$filename2 = $row['filename'];
   $myFile = "../photos/$filename2";
   $fh = fopen($myfile,'w') or die ("Can't open file");
   fclose($fh);
   unlink($myFile);
}

 $query3 = "DELETE FROM photos WHERE code='$code'";
    $result = mysql_query($query3)
    or die ("Couldn't execute query.");

}
I get this...
Warning: fopen() [function.fopen]: Filename cannot be empty in /home/beautyby/public_html/admin/price.php on line 39
Can't open file
And the line the error seems to be pointing to...

Code: Select all

$fh = fopen($myfile,'w') or die ("Can't open file");
kdidymus
Forum Contributor
Posts: 196
Joined: Tue May 13, 2008 3:37 am

Re: fopen() Problem

Post by kdidymus »

Okay. My bad.

REALLY simple typo...
$myFile = "../photos/$filename2";
$fh = fopen($myfile,'w') or die ("Can't open file");
Did you spot it? The second instance of $myFile (highlighted in red) had a lower-case F.

Aaaaarrrgggh!

Thank you Mikosiko. Your solution was spot on. If only my proof reading was up to scratch!
Post Reply