PDA

View Full Version : Why won't mkdir() work inside of a function?



Remix919
01-08-2012, 07:34 PM
I'm trying to create a directory using mkdir() but it seems to work fine outside the function, but when I put the code inside the function it won't work. Here is the code I'm using:



class Scrapper extends xhttp{

private function dl_images($images){

$getyear = date("Y");
$getmonth = date("m");
$getday = date("d");

$pickey = 1;
foreach($images AS $image):

$post_id = $this->db->insert_id;

$thumb_folder = "../classifieds/images/$getyear/$getmonth/$getday/$post_id/";

mkdir("$thumb_folder", 0755);

$image = str_replace("medium","large",$image);

if(!$this->is_image($image)) continue;

$img = file_get_contents($image);

$image_name = end( explode("/", $image ) );

file_put_contents( $thumb_folder . $image_name , $img );

$pickey++;

endforeach; // END OF LOOPING THROUGH IMAGES

}
}

felgall
01-08-2012, 07:45 PM
$thumb_folder doesn't have a value assigned to it inside the function. It is NOT the same $thumb_folder as exists outside the function.

tangoforce
01-08-2012, 08:31 PM
private function dl_images($images){
$thumb_folder = "../classifieds/images/$getyear/$getmonth/$getday/$post_id/";

mkdir("$thumb_folder", 0755);




$thumb_folder doesn't have a value assigned to it inside the function. It is NOT the same $thumb_folder as exists outside the function.

Really? - Could of fooled me!

@remix:

Your syntax is wrong. You have a : on the end of your foreach instead of a opening brace { and at the end you need a closing brace } not endforeach;

Spookster
01-08-2012, 09:00 PM
Really? - Could of fooled me!

@remix:

Your syntax is wrong. You have a : on the end of your foreach instead of a opening brace { and at the end you need a closing brace } not endforeach;


Really? - could of fooled me!

That is valid syntax. Just not commonly used.
http://php.net/manual/en/control-structures.alternative-syntax.php

tangoforce
01-08-2012, 09:38 PM
Really? - could of fooled me!

That is valid syntax. Just not commonly used.
http://php.net/manual/en/control-structures.alternative-syntax.php

I stand corrected!

_Aerospace_Eng_
01-09-2012, 12:24 AM
Where do you call your function from? You do see that it is a private function?

Remix919
01-09-2012, 03:03 PM
Yea, I got it working now though, the problem was I wasn't able to create a directory within a non-existent directory, for example if /home exists and I wanted to mkdir /home/test it works, but I couldn't do /home/sub/test unless I made /home/sub first.

Fou-Lu
01-09-2012, 04:23 PM
Yea, I got it working now though, the problem was I wasn't able to create a directory within a non-existent directory, for example if /home exists and I wanted to mkdir /home/test it works, but I couldn't do /home/sub/test unless I made /home/sub first.

That's logical. You can override this behaviour if necessary by setting the third parameter of mkdir to true (default is false).