PDA

Click to See Complete Forum and Search --> : PHP Uploader


silver767
Oct 30th, 2009, 06:59 PM
hey
i wanted to create form to upload files to my websites ftp.
But i dont know much about php, dunno if i should use php or html either.

Anyone got tips to give me?

i wanted this function for my website, its running php nuke 8.1

Thanks.

*EDIT*
i found this when searching around


<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>


but it seems to fail and comes with this messages


Warning: move_uploaded_file(uploads/Lekser.txt) [function.move-uploaded-file]: failed to open stream: No such file or directory in /users/silverlight76/www/uploader/uploader.php on line 6

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/users/silverlight76/temp/phpE3NZPe' to 'uploads/Lekser.txt' in /users/silverlight76/www/uploader/uploader.php on line 6
There was an error uploading the file, please try again!

the ftp supports uploading, got an ftp uploader made in vb, can it be fixed or can i drop that move_uploaded_file thingy?

SambaNeko
Oct 30th, 2009, 11:28 PM
This has nothing to do with FTP.

What does your directory structure look like? In particular, where is your "uploads" directory relative to uploader.php? You will also need to ensure that you have enabled write permission on the "uploads" directory - are you familiar with chmod?

silver767
Oct 31st, 2009, 07:13 AM
This has nothing to do with FTP.

What does your directory structure look like? In particular, where is your "uploads" directory relative to uploader.php? You will also need to ensure that you have enabled write permission on the "uploads" directory - are you familiar with chmod?

no im not familiar with that and i did set the permission to 777, i will try out some other stuff to

*EDIT* i got uploader to work, but
max file size dont seem to work, also files not allowed wont work either.
anyone know what could be it?
heres the code


<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}

if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>


i can still upload php and over 350kb files

silver767
Oct 31st, 2009, 11:06 AM
oh yeah i forgot to ask if theres a way to make it find out if theres an existing file with the same name? so i can make it cancel if the name already exist, or it will overwrite :S

kows
Oct 31st, 2009, 11:10 AM
I don't see where you ever define $uploaded_type or $uploaded_size, so of course they wouldn't work. you can use the information within $_FILES to define one of these variables:

$uploaded_size = $_FILES['uploaded']['size'];

however, the "type" passed in the $_FILES array is just a mimetype sent by the browser. this means that you shouldn't take it for granted, and instead you can get the actual file extension of the file to check whether or not it's "valid."

$extension = pathinfo($_FILES['uploaded']['name'], PATHINFO_EXTENSION);

$bad_extensions = array("php", "cgi", "html", "mp3", "exe");

if(in_array(strtolower($extension), $bad_extensions)){
echo "bad extension";
}else{
//move your file
}

you're also referencing two different $_FILES in your script; if your form's "file" input is named "uploaded," make sure every reference to $_FILES is "uploaded" (not "uploadedfile" like you've done at least once).

and yes, you can check if a file already exists by using the function file_exists().

if(file_exists($newpath)){
echo "filename already exists.";
}else{
//move file
}
a better way of doing this would be to tack a number onto the end of the file name, in a loop, until that file does not exist. this is only better if you'd rather it didn't "cancel."

silver767
Oct 31st, 2009, 11:58 AM
I don't see where you ever define $uploaded_type or $uploaded_size, so of course they wouldn't work. you can use the information within $_FILES to define one of these variables:

$uploaded_size = $_FILES['uploaded']['size'];

however, the "type" passed in the $_FILES array is just a mimetype sent by the browser. this means that you shouldn't take it for granted, and instead you can get the actual file extension of the file to check whether or not it's "valid."

$extension = pathinfo($_FILES['uploaded']['name'], PATHINFO_EXTENSION);

$bad_extensions = array("php", "cgi", "html", "mp3", "exe");

if(in_array(strtolower($extension), $bad_extensions)){
echo "bad extension";
}else{
//move your file
}

you're also referencing two different $_FILES in your script; if your form's "file" input is named "uploaded," make sure every reference to $_FILES is "uploaded" (not "uploadedfile" like you've done at least once).

and yes, you can check if a file already exists by using the function file_exists().

if(file_exists($newpath)){
echo "filename already exists.";
}else{
//move file
}
a better way of doing this would be to tack a number onto the end of the file name, in a loop, until that file does not exist. this is only better if you'd rather it didn't "cancel."

thank you very much, i will try it.
I got the php script from another site tutorial (php.about.com)

silver767
Oct 31st, 2009, 07:52 PM
ok i got the max size and file types allowed to work, thanks

but i didnt completely get the last to work, not sure what to do.
now i also wonder if there is a way to reverse the function of the bad extension, so i type in the file formats that are allowed.

kows
Oct 31st, 2009, 08:04 PM
well, yes, of course. it's doing the opposite of what the code I gave you before was. the code I gave you checks if in_array() returns true. you can either make this check if it returns false, or put your file-moving stuff in place of where the error message would go, and put the error message where the file-moving stuff would be now. to illustrate better, either:
$extensions = array("jpg", "gif", "png", "swf");

if(!in_array(strtolower($extension), $extensions)){
echo "bad extension";
}else{
//move your file
}



/**** or ****/

$extensions = array("jpg", "gif", "png", "swf");

if(in_array(strtolower($extension), $extensions)){
//move your file
}else{
echo "bad extension";
}

as far as the "last thing" goes, I'm guessing you're talking about tacking numbers onto file names. I won't help you with it unless you've actually made an attempt yourself. good luck.