Upload Questions<

liunx

Guest
I'm writing an upload script to go with my photo gallery that I made. I want to be able to have my wife or I choose a file (a photo), give a description, etc. Then have the script upload it to the web server, and add it to the database. I have a couple problems though.

First off, can I create a directory, and set the permissions with php? I have a /images/xavier directory...and the pictures are in sub directories under there, such as "at_the_hospital" or "at_home" or "one_month" and I'd like to make it so I can tell the script to upload the picture to a "two_months" directory, have it make that directory, and set it's perms to 777.

Also, Is there anyway to have a browse style box, that I can add so that the user (my wife) can see what direcories currently exist? (so we don't get directories such as "two_months" and another one that is "at_two_months").

Well, I guess that's it for now. I'm sure I'll have more questions later :)Almost forgot to post the code that I have, incase that helps.
The PHP file "upload_file.php":
<?PHP
//$userfile_size & $userfile_type are also valid variables
if($userfile=="none"){
echo "No file specified";
exit;
}
if (move_uploaded_file($userfile, "../temp/".$userfile_name)){
echo "Your file has been uploaded.";
} else {
echo "Could not upload file.";
}
?>

And the form as it is currently:

<form action="php/upload_file.php" method="post"
enctype="multipart/form-data" name="upload_pic" id="upload_pic">
Picture:
<input type="file" name="userfile" />
<br />
<input type="submit" name="Submit" value="Send" />
</form>yes you can create a folder with php.

use mkdir("/path/to/folder");

and then to chmod it you can use

chmod();

something like this

if (!file_exists($_SERVER['DOCUMENT_ROOT']."/temp/".$folder)){
$path = $_SERVER['DOCUMENT_ROOT']."/temp/".$folder;
$oldmask = umask(0);
mkdir ($path, 0777);
umask($oldmask);
$status = "Directory \"{$folder}\" made - <span style=\"color:green\"><b>OK</b></span><br />";

and look into the opendir() to browseOk, I checked out the opendir/readdir functions. I think that this is exactly what I want. I got it to read my images directory, and output only valid directories (no files, and no . or ..). You can see it here: <!-- m --><a class="postlink" href="http://campbells.dikaios.net/php/test.php">http://campbells.dikaios.net/php/test.php</a><!-- m -->
Now I want to make each of those into a link (which I think I can do), but what I want the link to do is run a php function. How can I do that? For example, if someone clicks on the xavier link, I want it to do $dir=$dir.$file.'/' that way, the next time through it'll use ../images/xavier/ as the new dir. It seems like this should be pretty simple. what am I missing?

<?php
if ($dir == NULL){$dir = "../images/";}

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (filetype($dir . $file)=="dir" && $file != '.' && $file!= "..") {
echo "filename: $file \n <br /> \n";
}
}
closedir($dh);
}
}
?>just have a index.php file in each directory. then you can have that to view the contents of that directory. other than that I am not sure how to run a function after you already went to that folder to view it.it's pretty simple, just make a link on the same file with ?dir=something after then you have $dir in your script to open up and list the file...

<?php
if (!isset($dir)){$dir = "../images/";}
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file!= "..") {
if (is_dir($dir.$file)) {
echo "<a href=http://www.htmlforums.com/archive/index.php/\"$php_self?dir=$dir$file\">dirname: $file</a> \n <br /> \n";
}
else {
echo "filename: $file \n<br />";
}
}
}
closedir($dh);
}
}
?>Thanks...I was missing a pretty simple solution. Since I am using a form anyway, I simply had it list the directory structure on the left, with each directory made into a link. The link, when clicked, fills in a textfield in the form called "directory"

Works great, thanks for all your guys' help. Now I have to implement a login, so not just anyone can upload.
 
Back
Top