简体   繁体   中英

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?

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.

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM