Solved

create directory with full permissions that will work in public server

Posted on 2010-11-23
15
255 Views
Last Modified: 2012-05-10
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?

0
Comment
Question by:blue-genie
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 3

Assisted Solution

by:xeonol
xeonol earned 50 total points
ID: 34202646
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34202742
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
 
LVL 4

Expert Comment

by:betopa
ID: 34202810
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 34202897
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34202899
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34202933
@DaveBaldwin - sorry I'm just a flash developer - what do you mean?
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
ID: 34202945
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Author Comment

by:blue-genie
ID: 34203033
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
 
LVL 4

Expert Comment

by:betopa
ID: 34205189
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34214143
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
 
LVL 4

Accepted Solution

by:
betopa earned 350 total points
ID: 34215116
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
 
LVL 4

Assisted Solution

by:betopa
betopa earned 350 total points
ID: 34215130
Sorry, the specs of the function:

http://php.net/manual/en/function.readdir.php
0
 
LVL 39

Author Comment

by:blue-genie
ID: 34215454
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34221916
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
 
LVL 39

Author Comment

by:blue-genie
ID: 34226288
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now