0

I have a table containing pictures URL that looks like this:

Table:
partno | url
10878  | http://www.pic1.jpg
20477  | http://www.pic2.jpg
and so on (+10000)

I want to run a script in order to download all of them on my LOCAL path

I was confusing what could be the most simple way to do it. I wanted to run a PHP script in order to get the URLs from MySQL and then use a PHP command to download each of them, something like this ...please help me

below .. i have done one by one download image by using textbox to search and download

// Open the file to get existing content
$data = file_get_contents($file);
$newimage=basename($file);

// New file
$new = 'image/'.$newimage;
// Write the contents back to a new file
file_put_contents($new, $data);
mysql_query("Insert into image set name = '$newimage' ");
echo "<img src='image/$newimage' width='300px' height='250px'/>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="url" name="url"/><br/><br/>
<input type="submit" name="subimg" value="submit"/> 
 </form>

multiple image download from URL stored in database code..but output will not come..please help me

 <?php
 // connection to database
 $con = mysql_connect("localhost","root","");
 mysql_select_db('import_exceldata',$con) 
 ?>
 <?php
if(isset($_REQUEST['subimg']))
{
$result=mysql_query("select url from mytask ");
set_time_limit(1000);    
while( $url = $result->fetch() ) {  
// Open the file to get existing content
 $data = file_get_contents($result);
 $newimage=basename($result);
// New file
$new = 'image/'.$newimage;
// Write the contents back to a new file
file_put_contents($new, $data);
}
mysql_query("Insert into image set name = '$newimage' ");
echo "<img src='image/$newimage' width='300px' height='250px'/>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="submit" name="subimg" value="retrieve images"/> 
</form>
j.mani
  • 3
  • 6

2 Answers2

0

I'm not sure if i understood right, but you can get your file url's from mysql via the sourcecode below. This would store your data from the table in $row, which you can use. And it echos the whole $row, so you can see the result of the query.

<?php
$link = mysql_connect('localhost', 'username', 'password');
$db_selected = mysql_select_db('database_name', $link);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$result = mysql_query('SELECT partno, url FROM table_name');
if (!$result) {
    die('Error: ' . mysql_error());
}

//to show what the query got
while ($row = mysql_fetch_assoc($result)) {
    echo ("name: ").$row['partno'].("<br>");
    echo ("url: ").$row['url'].("<br>");
}
//to store the data in $row
$row = mysql_fetch_assoc($result)
mysql_close($link);
?>
neverlucky
  • 35
  • 2
  • 10
  • this code only show table in all rows.. i want to fetch url one by one and the url run to get multiple image to stored in local path – j.mani Jan 03 '17 at 11:40
  • do u know that pls tel me – j.mani Jan 03 '17 at 11:42
  • Works for me with my table in my mysql database. Did you replace the placeholders? username/pw/... Otherwise i don't understand what you mean. If you want to access $row one by one you could use a for-loop – neverlucky Jan 03 '17 at 13:58
0

I will suggest you to use python for this task and write following code

import MySQLdb
import urllib

con = MySQLdb.Connect('server', 'username', 'password', 'database')
cursor = con.cursor()
query = "SELECT url FROM table"
cursor.execute(query)
result = cursor.fetchall()
for url in result:
   urllib.urlretrieve(url[0], "local/address/filename")
Cybersupernova
  • 1,833
  • 1
  • 20
  • 37