尝试阻止文件在用户目录中重复。这就是我的想法,
$data = $connection->getMedia($fileid);
/**
* The test begins
*/
$dir = 'media/download/'.$username;
$files1 = scandir($dir);
foreach ($files1 as $i => $file) {
if (md5($file) == md5($data)) {
//statement
echo "Duplicate entry detected.";
}
else {
file_put_contents($finalPath, $data);
}
然而,这给了我无限循环,我无法看到它在做什么。那么,任何人都可以给我一个更好的主意吗?
编辑:完整代码:
foreach ($accounts as $username => $password) {
$connection= new Connection($username,$password);
$fileList= $connection->getFiles();
echo '<br><p>Last available files for '.$username.': <br>';
if (!file_exists('media/download/'.$username)) {
mkdir('media/download/'.$username, 0777, true);
}
for ($i=0;$i<7;$i++)
{
$fileID= $fileList[$i]->id;
$data = $connection->getMedia($fileID);
if (!strlen($data) < 1 ) {
$recipient = $fileList[$i]->recipient;
$finalPath = 'media/download/'.$username.'/to'.$recipient.'_id'.$fildID.rand(0,200).'.jpg';
file_put_contents($finalPath, $data);
echo "<a href=".$finalPath."> <img src='".$finalPath."' height='100' width='100'> </a>";
}
}
}