I need to download some images from a specific array of urls, something like:
<?php
$images = array('http://url1.com/img1.png','http://url1.com/img2.png');
// download this images from this paths
I saw some scripts here but don't get them exactly on how can i link them to this array.
The expected output would be: When i run the script to download those images from that array to a specific folder from my server: home/user/public_html/images. It's much appreciated anyone who helps me, i am trying but can't make the connections, currently a rookie.
Something like this maybe.
$images = array('http://ecx.images-amazon.com/images/I/214RgVjsvTL.jpg','http://ecx.images-amazon.com/images/I/515pMJlul8L.jpg');
foreach($images as $name=>$image) {
//get image
$imageData = file_get_contents($image); //$image variable is the url from your array
$name = explode("/", $image);
$handle = fopen("images/".$name[5],"x+");
fwrite($handle,$imageData);
fclose($handle);
}
Related
I'm trying to save a jpg file from a dynamic url, looks like this,
http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api
file_get_contents can't get the content correctly, here is my code,
<?php
$image_url = "http://bks7.books.google.se/books?id=TL3JGsUOArkC&printsec=frontcover&img=1&zoom=1&&source=gbs_api";
$img = file_get_contents($image_url);
$folder = 'C:/xampp/htdocs/test/test.jpg';
file_put_contents($folder, file_get_contents($img));
?>
Appreciate any ideas or alternative as "easy" methods.
One problem is that you have 2 file_get_contents calls. The first call:
$img = file_get_contents($image_url);
Returns the response from the request to the URL and stores it in the $img variable. The second call:
file_put_contents($folder, file_get_contents($img));
Doesn't make any sense. Instead, just do this:
file_put_contents($folder, $img );
I have a small problem: I want to show images from directory, what I successfully programmed, but just from directory which has a static name. How can I add here some filter, or something like that, when I send in variable osc number 31322357 it will automatically find directory 31322357 - Automatic Board and show images? Thanks.
Here is my script:
<?
//path to directory to scan
$osc=$_GET['osc'];
$directory="./$osc/";
//get all image files with a .jpg extension.
$images = glob("$directory{*.jpg,*.JPG,*.png}", GLOB_BRACE);
//print each file name
foreach($images as $image)
{
echo "<img src='$image' style='width:100px;height:auto'>";
}
?>
<?php
...
$dirs = glob("./$osc*",GLOB_ONLYDIR);
$images = array();
if(count($dirs))
foreach($dirs as $dir){
$images = array_merge($images,glob("$dir/{*.jpg,*.JPG,*.png}", GLOB_BRACE));
}
?>
Use a wildcard * in your directory name:
$directory = "./$osc*/";
$images = glob("$directory*.{jpg,JPG,png}", GLOB_BRACE);
Note that this will match 31322357 - Automatic Board and 31322357 - Other stuff, etc. and list files from all matching directories.
I am a newbie at PHP and I'm learning.
I've made a basic script where you can upload an image to a director on the server. I want the image names to get a number at the end so that the name won't be duplicated.
This is my script to add 1 to the name (I'm really bad at "for loops"):
for(x=0; $imageName => 50000; x++){
$imageFolderName = $imageName.$x;
}
Please tell me if I'm doing this totally wrong.
Adding to Niet's answer, you can do a foreach loop on all the files in your folder and prepend a number to the file name like so:
<?
$directory = 'directory_name';
$files = array_diff(scandir($directory), array('.', '..'));
$count = 0;
foreach($files as $file)
{
$count++;
rename($file, $count.'-'.$file);
}
?>
Alternatively you could rename the file to the timestamp of when it was uploaded and prepend some random characters to the file with the rand() function:
<?
$uploaded_name = 'generic-image.jpeg';
$new_name = time().rand(0, 999).$uploaded_name;
?>
You'll need to handle and move the uploaded files before and after the rename, but you get the general gist of how this would work.
Here's a potential trick to avoid looping:
$existingfiles = count(glob("files/*"));
// this assumes you are saving in a directory called files!
$finalName = $imageName.$existingfiles;
im pretty new to PHP scripting, and i couldnt find the answer to my desires yet, so i was hoping someone could tell me how to do this.
Im getting a $_POST request to a php file, containing some data
stock_nr=1&manufacturer=Audi&model=A1&images=http%3A%2F%2Fimages0.wheeler.nl%2Fs1386081049%2F6911582-1-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081072%2F6911582-2-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081102%2F6911582-3-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081131%2F6911582-4-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081161%2F6911582-5-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081192%2F6911582-6-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081227%2F6911582-7-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081255%2F6911582-8-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081280%2F6911582-9-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081313%2F6911582-10-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081341%2F6911582-11-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081368%2F6911582-12-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081398%2F6911582-13-2-2.jpg
I have made a file to declare variables like this:
$stock_nr=$_POST['stock_nr'] ;
$manufacturer=$_POST['manufacturer'] ;
$model=$_POST['model'] ;
$images=$_POST['images'] ;
How can i get the amount of imagelinks which are posted in a variable?
Edit: This is what i wanted to do, and it works fine.
This is what i wanted to do, and it does what it should do:
function process_images() {
$images = explode(',', $_POST['images']);
foreach($images as $image_nr => $image_url) {
$filename = 'images/'. $_POST['stock_nr'] .'-'. $image_nr .'.jpg';
$imgdata = file_get_contents($image_url);
file_put_contents($filename, $imgdata);
}
}
These links are separated by "," you just need to explode() it.
$images=explode(',',$_POST['images']);
print_r($images);
I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.