need help with my phpupload script

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Jayreis
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jul 25, 2003 8:36 am
Contact:

need help with my phpupload script

Postby Jayreis » Fri Jul 25, 2003 8:40 am

Hello,

I have a php upload script which partly works (it does upload a file to a premade and selected folder) although it gives me an error. So here is my problem in my form I want the option of ieather selecting the folder to upload to (which currently works) OR the option just to type a name of a folder that the upload.php would create and then upload the file to.

my issue is that the script is not creating the folder that I type into the text box named directory. so I need to know what to change in my script below to make that part work.

thanks

<?

$temp ="/tmp"; # unix system temp dir
$maxsize ="40960"; # max 40 Kb
$quota = 524288; # define space quota 500 Kb
$ext = array(".p", ".php", ".php3", ".phtml", ".cgi", ".jsp", ".pl", ".shtml"); # define file extension to reject

if($directory!='')
mkdir("./projects/$directory",0755);

if ($userfile AND $userfile != "none") {

$total=0;
$handle=opendir($dir);
while ($file = readdir($handle)) {
if (is_file("$dir/$file")) {$total+=filesize("$dir/$file");}
}
while (list($key,$val) = each($ext)) {
if (strstr($userfile_name, $val)) {$invalidext=true;break;}
}
if ($userfile_size > $maxsize) {echo "ERR: File too large";}
elseif ($invalidext) {echo "ERR: Forbiden file extension";}
elseif ($total > $quota) {echo "ERR: Space quota exceeded";}
else {

rename("$userfile", "$temp/$userfile_name");
copy("$temp/$userfile_name", "./projects/$dir/$userfile_name");
unlink("$temp/$userfile_name");
echo "Your file has been uploaded succesfully";
}
} else {
?>
<form action="<?echo $PHP_SELF?>" method=POST ENCTYPE="multipart/form-data">
<table>
<tr>
<td class=navbox>Select File:
<input type=file name=userfile><br> Create Directory<input type=text name=directory> or Directory Name:<select name=directory><option></option><option>music</option><option>pictures</option>
</select><br><br><input type=submit value=Upload></td>
</tr>
</table>
</form>
<?
}
?>

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Fri Jul 25, 2003 10:04 am

surely calling the select $directory as well will effectively overwrite the input text with ""

try naming the text field name="make_directory"

then

Code: Select all

$dir = $_POST['directory'];
if($_POST['make_directory'] !== "")
 {
 mkdir('./projects/' .$_POST['make_directory'],0755);
 $dir = $_POST['make_directory'];
 }

Jayreis
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jul 25, 2003 8:36 am
Contact:

..........

Postby Jayreis » Fri Jul 25, 2003 10:58 am

Hello,

OK thanks for your help although now it always creates the /projects/ folder and then just uploads the file to their it still does not create a folder name. which is what I am trying to do I have a drop down form that dynamicly will list the dirs in their and I have a text box. and what I want is if the person does not want to upload to a folder they select via the drop down box they then can type a name in the text box to create a folder.

let me know
thanks

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Fri Jul 25, 2003 1:12 pm

so you named the form fields differently?

I presume you've tried echoing the $_POST variables to check they exist?

do a

var_dump($_POST);

you should have a value for 'make_directory' if you typed anything, or a value for 'directory' if you selected something

Jayreis
New php-forum User
New php-forum User
Posts: 3
Joined: Fri Jul 25, 2003 8:36 am
Contact:

..............

Postby Jayreis » Fri Jul 25, 2003 5:05 pm

ok here is what I am using tell me where I am wrong plz.

(I renamed the text box name and the drop down to make_directory, they both have to be named the same thing right?)

<?

$temp ="/tmp"; # unix system temp dir
$maxsize ="40960"; # max 40 Kb
$quota = 524288; # define space quota 500 Kb
$ext = array(".p", ".php", ".php3", ".phtml", ".cgi", ".jsp", ".pl", ".shtml"); # define file extension to reject

$dir = $_POST['directory'];
if($_POST['make_directory'] !== "")
{
mkdir('./projects/' .$_POST['make_directory'],0755);
$dir = $_POST['make_directory'];
}

if ($userfile AND $userfile != "none") {

$total=0;
$handle=opendir($dir);
while ($file = readdir($handle)) {
if (is_file("$dir/$file")) {$total+=filesize("$dir/$file");}
}
while (list($key,$val) = each($ext)) {
if (strstr($userfile_name, $val)) {$invalidext=true;break;}
}
if ($userfile_size > $maxsize) {echo "ERR: File too large";}
elseif ($invalidext) {echo "ERR: Forbiden file extension";}
elseif ($total > $quota) {echo "ERR: Space quota exceeded";}
else {

rename("$userfile", "$temp/$userfile_name");
copy("$temp/$userfile_name", "./projects/$dir/$userfile_name");
unlink("$temp/$userfile_name");
echo "Your file has been uploaded succesfully";
}
} else {
?>
<form action="<?echo $PHP_SELF?>" method=POST ENCTYPE="multipart/form-data">
<table>
<tr>
<td class=navbox>Select File:
<input type=file name=userfile><br> Create Directory<input type=text name=directory> or Directory Name:<select name=directory><option></option><option>music</option><option>pictures</option>
</select><br><br><input type=submit value=Upload></td>
</tr>
</table>
</form>
<?
}
?>


I am also getting this error now when the page first loads:
Warning: mkdir(./projects/): File exists in /home/smedia/public_html/projects12/microvisuals/upload.php on line 12

and after I try to upload something it just uploads the stuff to /Project/ directory even if I select a directory from the drop down it still uploads now only to the projects directory

and the will give me this error
Warning: mkdir(./projects/): File exists in /home/smedia/public_html/projects12/microvisuals/upload.php on line 12

Warning: readdir(): supplied argument is not a valid Directory resource in /home/smedia/public_html/projects12/microvisuals/upload.php on line 20
Your file has been uploaded succesfully

plzz help thanks

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Sat Jul 26, 2003 2:19 am

ok - my codes use reg_globals OFF (which is default since php4.2) - yours use the older approach of accessing post/file/get etc variables through the global scope - eg $varname rather than $_POST['varname'];

either recode the bit I did to use the older method (which undoubtedly will have to be updated as soon as your host switches reg_globals off)

or update all your code bits to access the variables through the $_FILES or $_POST array scopes.
Read this [age to learn how to access the files array variables.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest