Alexandra22
Alexandra22

Reputation: 3

How to make file_exists to Work(Php)?

I have made this code, to save images to a remote directory, but i have not an Idea where to code do nothing if file exists:

  $album_name = $row['album'];
if(file_exists("cdcovers/$album_name.jpg")){
    <---here
 } 

else {
//save images
$imageString =       file_get_contents(LastFMArtwork::getArtwork($row['artist'], $row['album'],    true, "large"));
$save = file_put_contents('/home/link/public_html   /cdcovers             /'.$row['album'].'.jpg',$imageString);
}   

Can i have some ideas?

Upvotes: 0

Views: 61

Answers (4)

xyLuz
xyLuz

Reputation: 198

You an either leave the "if" block blank and write your working code in the "else" block or you can go with @keith and @thomasw_lrd answers.

Or you can give a "file already exist" message in the "if" block.

Upvotes: 0

Duane Lortie
Duane Lortie

Reputation: 1260

remove the "<---here" and you are done...

if(blah==TRUE){ 
/*do nothing */
} ELSE {
/*do something*/  
code to do something goes here 
}

Upvotes: 0

thomasw_lrd
thomasw_lrd

Reputation: 546

This is one of the few times I would use a negative if statment

 $album_name = $row['album'];
if(!file_exists("cdcovers/$album_name.jpg")){
  //save images
  $imageString = file_get_contents(LastFMArtwork::getArtwork($row['artist'], $row['album'],    true, "large"));
  $save = file_put_contents('/home/link/public_html   /cdcovers /'.$row['album'].'.jpg',$imageString);
} 

I feel it makes the code cleaner, and easier to read.

Upvotes: 0

Keith Rockhold
Keith Rockhold

Reputation: 124

if(!file_exists("cdcovers/$album_name.jpg")){
    //save images
    $imageString = get_contents(LastFMArtwork::getArtwork($row['artist'], $row['album'],    true, "large"));
    $save = file_put_contents('/home/link/public_html   /cdcovers             /'.$row['album'].'.jpg',$imageString);
}   

If the file does NOT exist, save it. It will do nothing if it does.

Upvotes: 1

Related Questions