Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP通过url上传1000幅图像_Php_Download_Uploading - Fatal编程技术网

使用PHP通过url上传1000幅图像

使用PHP通过url上传1000幅图像,php,download,uploading,Php,Download,Uploading,我想通过URL一次点击上传1000张图片。我在MYSQL数据库中存储了1000个图像URL 所以请任何人给我PHP代码,通过mysql数据库通过URL上传这1000张图片 目前我正在使用以下代码:- 它通过发布图像的URL,每次点击上传一个图像 但我想通过从数据库中获取URL,一次点击上传1000张图片 $result = mysql_query("SELECT * FROM thumb") or die(mysql_error()); // keeps getting the next ro

我想通过URL一次点击上传1000张图片。我在MYSQL数据库中存储了1000个图像URL

所以请任何人给我PHP代码,通过mysql数据库通过URL上传这1000张图片

目前我正在使用以下代码:- 它通过发布图像的URL,每次点击上传一个图像

但我想通过从数据库中获取URL,一次点击上传1000张图片

$result = mysql_query("SELECT * FROM thumb") or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

echo "<div>";
$oid = $row['tid'];
$th= $row['q'];

echo "</div>";

$thi = $th;
$get_url = $post["url"];
$url = trim('$get_url');

if($url){
    $file = fopen($url,"rb");
    $directory = "thumbnail/";
    $valid_exts = array("php","jpeg","gif","png","doc","docx","jpg","html","asp","xml","JPEG","bmp"); 
    $ext = end(explode(".",strtolower(basename($url))));
    if(in_array($ext,$valid_exts)){

        $filename = "$oid.$ext";
        $newfile = fopen($directory . $filename, "wb");
        if($newfile){
            while(!feof($file)){
                fwrite($newfile,fread($file,1024 * 8),1024 * 8);
            }
            echo 'File uploaded successfully';
            echo '**$$**'.$filename;
        }
        else{
            echo 'File does not exists';
        }
    }
    else{
        echo 'Invalid URL';
    }
}
else{
    echo 'Please enter the URL';
}

}
$result=mysql\u query(“从拇指中选择*”)或die(mysql\u error());
//一直排到下一排,直到没有更多的东西了
while($row=mysql\u fetch\u数组($result)){
回声“;
$oid=$row['tid'];
$th=$row['q'];
回声“;
$thi=$th;
$get_url=$post[“url”];
$url=trim(“$get_url”);
如果($url){
$file=fopen($url,“rb”);
$directory=“thumbnail/”;
$valid_exts=数组(“php”、“jpeg”、“gif”、“png”、“doc”、“docx”、“jpg”、“html”、“asp”、“xml”、“jpeg”、“bmp”);
$ext=end(explode(“.”),strtolower(basename($url));
if(在数组中($ext,$valid\u exts)){
$filename=“$oid.$ext”;
$newfile=fopen($directory.$filename,“wb”);
如果($newfile){
而(!feof($file)){
fwrite($newfile,fread($file,1024*8),1024*8);
}
echo“文件上传成功”;
回显“**$$**”。$filename;
}
否则{
echo“文件不存在”;
}
}
否则{
回显“无效URL”;
}
}
否则{
echo“请输入URL”;
}
}

非常感谢

您的代码已经过时,而且比需要的复杂得多。这不是一个你可以通过提问获得代码的网站,这是一个学习环境

我给你举个例子,你可以继续:

// Select the images (those we haven't done yet):
$sItems = mysql_query("SELECT id,url FROM thumb WHERE imported=0") or die(mysql_error());
// Loop through them:
while( $fItems = mysql_fetch_assoc($sItems) ){
    $imgSource = file_get_contents($fItems['url']); // get the image
    // Check if it didn't go wrong:
    if( $imgSource!==false ){
        // Which directory to put the file in:
        $newLocation = $_SERVER['DOCUMENT_ROOT']."/Location/to/dir/"; 
        // The name of the file:
        $newFilename = basename($fItems['url'], $imgSource); 

        // Save on your server:
        file_put_content($newLocation.$newFilename);
    }
    // Update the row in the DB. If something goes wrong, you don't have to do all of them again:
    mysql_query("UPDATE thumb SET imported=1 WHERE id=".$fItems['id']." LIMIT 1") or die(mysql_error());
}
相关功能:
-获取图像的内容
-将此函数中给出的内容放入指定的文件中
-给定一个url,它只提供文件名

重要提示:

  • 您正在使用mysql\u查询。这已被弃用(不应再使用),请改用PDO或mysqli
  • 我建议您从命令行执行此操作,并在更新后添加一个echo,以便可以监视进度

更新了while循环中的部件,这应该足够了。我给了您一个示例。我已经为您提供了所需的所有信息和文档,代码几乎可以正常工作,尽管我并不打算这样做。如果你不明白我的答案,你可能想找一个懂的人。这可能会非常错误。+1不只是给别人答案,而自己却没有尝试去做。我觉得有太多的人仍然这样做。我可能会注意到,在数据库中存储图像不是一个好的做法。这不是图像,而是图像URL和在filemanager中上载