0

I tried making a script that could save all files from a webpage with a pattern in name to a server.

<?php

$base_url = "http://www.someurl.com/";
$sufix = ".jpg";
$id_start= "Name Numberstart"; //Picture 1 
$id_end = "Name Numberend"; // Picture 235
$path_to_save = "filer/";

foreach(range($id_start, $id_end) as $id) {
    file_put_contents($path_to_save.$id.$sufix, file_get_contents($base_url.$id.$sufix));
}

?>

If I only use id with numbers etc. 1-20 or 50 - 60 and so on, the script works perfectly. But when I have a name in front and white space, it doesn't seem to work. It just saves 1 file and stops.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
fixern256
  • 23
  • 5
  • Maybe [httrack](https://www.httrack.com/) is an option? I don't know what you purpose is, but this program downloads a website for you. – AgeDeO May 21 '15 at 15:13
  • Apply an str_replace or trim if the string starts with a whitespace ? Describe the 'name in front' case more in details please. – Answers_Seeker May 21 '15 at 15:14
  • why not keeping the id_start and id_end numeric and use the name that you want to prefix in the loop like { file_put_contents($path_to_save.'Name'.$id......} – Rakesh Shewale May 21 '15 at 15:20
  • @fixern256, please upvote and mark correct answers when they're posted. We're happy to help, but it's good form to thank people by upvoting their answers. – Byte Lab May 21 '15 at 15:39
  • Hi, would love to upvote, but I cant do this before I have 15 in reputation and this I dont have yet. So Upvoting must be done by other members that has this possibility :) – fixern256 May 21 '15 at 19:07
  • You are allowed to mark an answer as correct though, are you not? – Byte Lab May 29 '15 at 19:40

1 Answers1

0

According to php.net's description of range:

Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

I take that to mean that

range("Name Numberstart", "Name Numbersend") == array("N");

And if we run php interactively, this is confirmed:

php > $var = range("Name Numberstart", "Name Numbersend");
php > var_dump($var);
array(1) {
  [0]=>
  string(1) "N"
}

So basically, you're actually getting the range of the first character of the first word to the first character of the second word. If we change the second word to "Pretty", we get:

php > var_dump(range("Name Numberstart", "Pretty Numbersend"));
array(3) {
  [0]=>
  string(1) "N"
  [1]=>
  string(1) "O"
  [2]=>
  string(1) "P"
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Byte Lab
  • 1,576
  • 2
  • 17
  • 42