create directory with full permissions that will work in public server

i'm creating a folder images with subfolders using the data

if (!mkdir($structure,'777', true) ) {
    die('Failed to create folders...');
}

the images folder is created and i can create a subfolder 20101122 for example which i can then get the files directory and display the images in there using xampp on my local machine, however when I upload it to my webserver, it fails to work.

if I try accessing the path via the browser i.e. ... images/20101122 it tells me I don't have access - permissions error. - what am I missing?

LVL 39
blue-genieAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
betopaConnect With a Mentor Commented:
You are welcome.
Now, about the solution the ISP chat-guy gave you:
It is just not posible!
I dont think that the guy from the chat made a good job helping you.
You cant "cheat" admin privileges (or at least it is not THAT EASY). Your code is fine! But the folder will always be created with YOUR limitations defined by the server admin...

I strongly recomend a php script that displays what I think you want, the way you want it.
You could read the folder files, and print them in the browser:

<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>
0
 
xeonolConnect With a Mentor Commented:
The '777' will be recongized as a decimal number, see the reference of mkdir function http://php.net/manual/en/function.mkdir.php.

You may want to update the parameter of permission mode to use octal number:
mkdir($structure, 0777, true)

0
 
blue-genieAuthor Commented:
i used 0777 , i tried changing it to 777 because that's what it looks like in the folder properties, sorry I should have changed that before pasting it here. but changing to 0777 doesn't work either.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
betopaCommented:
I think maybe its because of the server configuration. You could not have the privileges needed.
Have you an admin user? You could check the permiissions in a shell window after you run your app on the web server. Even if you put the 0777 config, it could be creating it with another configuration.
0
 
Dave BaldwinFixer of ProblemsCommented:
You probably need to be logged in like you are in FTP.  All of my PHP upload pages use FTP thru PHP to accomplish that.
0
 
blue-genieAuthor Commented:
yeah busy googling at the moment and seems to be something to do with apache access / privelages.

but there's hundreds of different suggestions and I have no clue . tried adding stuff to .htacess but that's not happening.
0
 
blue-genieAuthor Commented:
@DaveBaldwin - sorry I'm just a flash developer - what do you mean?
0
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
See the user notes here: http://php.net/manual/en/function.mkdir.php  You can still have problems because the PHP/web process doesn't run under your user.  It runs under the user for Apache.  Which of course doesn't mean anything under Windows but it does on Linux.
0
 
blue-genieAuthor Commented:
ok cool that looks promising. will need to wait till I get home to test properly (with the webcam) will revert to you asap.
tx.
0
 
betopaCommented:
On the other side, it could be dangerous to mess with the apache user and security configurations.

You probably dont need to see the folder through the url in the browser, but you could do a script to display them. Watch this, specially the 2nd method:

http://www.mysql-apache-php.com/fileupload-security.htm
0
 
blue-genieAuthor Commented:
hey guys still no joy.
please have a look at the code attached, where do i put the chmode (bearing in mind i'm not a php developer) - this file is a combination of various tutorials ... this works in xampp on my machine, however on various live servers, the permissions aren't working. I chatted with my service provider and I have to overwrite it, and apparently Chmod is the way, but I can't get it to work ...

@betopa - in terms of security etc - thanks for that. Will definately point it out to the server admin guys.

 
<?php


$uid = $_GET[uid];
$structure = './images/' .  date("Ymd") .'/' . $uid. '/';

if(is_dir($structure))
{
echo "Already exists!";
}
else
{
echo "Doesn't exist" ;

if (!mkdir($structure,'0777', true) ) {
    die('Failed to create folders...');
}
} 


if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
	$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
	$img = $_GET["img"];
	$filename = 'images/' . date("Ymd"). "/" .$uid. "/img_". mktime(). ".jpg";
	file_put_contents($filename, $jpg);
} else{
	echo "Encoded JPEG information not received.";
}
?>

Open in new window

0
 
betopaConnect With a Mentor Commented:
Sorry, the specs of the function:

http://php.net/manual/en/function.readdir.php
0
 
blue-genieAuthor Commented:
betopa, just so that i understand, you mean to keep the php code as is and use that code you provided now to access it?
i'll have to try it over the weekend. thanks.
0
 
blue-genieAuthor Commented:
betopa i can't get this to work.
please help!
i get as far as writing the folder, can't get into the folder on my webserver to write to it or read to it.

0
 
blue-genieAuthor Commented:
ok all i manged to get it right. it was a case of my logic failed in terms of overwriting existing folders etc. and i also needed to use chmod.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.