简体   繁体   English

如何使file_exists工作(Php)?

[英]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. 您可以将“ if”块留空,然后在“ else”块中编写工作代码,也可以使用@keith和@thomasw_lrd答案。

Or you can give a "file already exist" message in the "if" block. 或者,您可以在“ if”块中给出“文件已存在”消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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