使用PHP通过URL上传1000张图像 [英] Uploading 1000 images via url using PHP
问题描述
我想通过URL一键上传1000张图像。我在MYSQL数据库中存储了1000个图像URL。
I want to upload 1000 images in just one click via URL. I have 1000 Image URLs stored in MYSQL database.
因此,请任何人给我PHP代码,以通过mysql数据库通过URL上传那1000个图像。
So please any one give me PHP code to upload that 1000 images via URL through mysql database.
当前我正在使用以下代码:-
通过发布图片的网址,每次点击上传一张图片...
Currently I am using the bellow code:- It upload one image per click by posting URL of image...
但是我想通过从databse获取URL一键上传1000张图片
But i want to upload 1000 image in one click by getting URLs from databse
$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';
}
}
非常感谢...。 …
推荐答案
您所拥有的代码已经过时,而且比需要的要复杂得多。这不是您获取代码的站点,因为您询问,这是一个学习环境。
The code you have is outdated and a lot more complecated than needed. This is not a site where you get code because you ask, this is a learning environment.
我给你一个可以继续的例子:
I'll give you an example on which you can continue:
// 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());
}
相关功能:
file_get_contents()-获取图像的内容
file_put_contents()-将此函数中给出的内容放入指定的文件中
basename()-给定一个url,它只为您提供文件名
Relevant functions:
file_get_contents() - Get the content of the image
file_put_contents() - Place the content given in this function in a file specified
basename() - given an url, it gives you the filename only
重要提示:
- 您正在使用mysql_query。不建议使用(不再使用),请改用PDO或mysqli
- 我建议您从命令行进行此工作,并在更新后添加回显,以便您可以监视进度
这篇关于使用PHP通过URL上传1000张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!