Log in

View Full Version : Uploading System



WebMaker12
08-31-2007, 02:30 PM
Hello Everyone,

I was looking for a script where users can upload files to the website and it would store them maybe in the FTP or somewhere private where only the respective admin would have access to download it?

like for example: www.yousendit.com you can upload files and you can decide whether you need to be a registered user to download your file or not. Would this be a HTML script? If not, what kind of script would I be looking for?

Thanks for your help

boogyman
08-31-2007, 05:42 PM
this could not be any type of client-side programming languages. you would need a server-side language such as (PHP/ASP) and some of database to store users / access codes.

WebMaker12
08-31-2007, 07:01 PM
(PHP/ASP) and some of database to store users / access codes.

I run PHP off the website.

As for the website, you need to have a login name to login into the website.


Like I could have users attach files but I would have to set it private and all.

So there is no easy way to do this? Only way possible is kind of like a forum where people can post attachments?

Thanks

boogyman
08-31-2007, 07:25 PM
okay so php is installed? how about a database? MySQL would probably be the easiest and its free.

and no you do not have to set everything "private" and all. you can make the site viewable to the world or only to certain people to login, or what would be ideal is to offer a "level" type system where you have some services for free, and you have prioritized / enhanced services for people whom have an account, now whether that be a paid account or not is again up to you. Its not really a matter of how "easy" it is, its a matter of your experience and knowledge.

If you do not have any experience working with this type of project, and you intend this to be a success and growing site, you probably would be better just using a model like phpBB which has a built in framework and offers all of the scripts you would need for this type of project... however if this is a developmental / educational site for you to learn how to do what you are asking it would probably take you a long time to write the scripts but yes its possible. Easy? probably not, but feasible nonetheless.

it really just depends on what level of committment, your time to schedule for getting the site operational, your intentions for the site, etcetc...
if you are building the site yourself there are numerous scripts on the web that will tell you how to write a file to your server, and check for some basic arguments and security concerns. do a quick search on google, and I also think Twey has something on his site that depicts something like this

WebMaker12
08-31-2007, 07:51 PM
okay so php is installed? how about a database? MySQL would probably be the easiest and its free.


Yes, PHP is installed. (I use Drupal) And I use MySQL




If you do not have any experience working with this type of project, and you intend this to be a success and growing site, you probably would be better just using a model like phpBB which has a built in framework and offers all of the scripts you would need for this type of project... however if this is a developmental / educational site for you to learn how to do what you are asking it would probably take you a long time to write the scripts but yes its possible. Easy? probably not, but feasible nonetheless.

it really just depends on what level of committment, your time to schedule for getting the site operational, your intentions for the site, etcetc...
if you are building the site yourself there are numerous scripts on the web that will tell you how to write a file to your server, and check for some basic arguments and security concerns. do a quick search on google, and I also think Twey has something on his site that depicts something like this
[/QUOTE]

I appreciate the time you took to write this out and give me some ideas. I got some ideas from what you said and I will apply them and then come back and tell you guys the way I did it... incase someone has a similar question.

Thanks

eleven82
08-31-2007, 08:01 PM
Here's a link to a tutorial I did once. It allows a user to upload something to a folder you create on your server. It worked for me... http://www.tizag.com/phpT/fileupload.php

WebMaker12
08-31-2007, 08:03 PM
Awesome!!

Thanks a lot

Here is what i've done so far.






(That is the basic part)

But here is where I am lost:


// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name'];

Lets say I want the files to be stored in: http://www.biblical-education.com/upload/files and from there I can open that file in my FTP and am able to see the uploaded files.

Where do I add that part of the line? Would I replace it with the "uploads/filename.extension"?

Also

$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!";
}
I understand the last line for the error... but the if statement confuses me -- What would I need to change here?

Sorry for the confusion I might of caused.

Thanks

thetestingsite
09-01-2007, 02:53 AM
But here is where I am lost:



// Where the file is going to be placed
$target_path = "uploads/";

/* Add the original filename to our target path.
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);


Lets say I want the files to be stored in: http://www.biblical-education.com/upload/files and from there I can open that file in my FTP and am able to see the uploaded files.

Where do I add that part of the line?



You would change this:



// Where the file is going to be placed
$target_path = "uploads/";


to point to the directory in which you want users to upload to. So in this case, it would be:



$target_path = 'upload/files/';







$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!";
}

... the if statement confuses me -- What would I need to change here?


You do not need to change anything here. The if statement basically says that if the uploaded file is uploaded (or has been moved from a temporary place to the final destination), to continue and show that the file has infact been uploaded. Otherwise, display the error.

Hope this helps.

WebMaker12
09-01-2007, 03:02 AM
Got it working!!!


Thanks a lot for your help guys.

keep up the good work here

Rockonmetal
09-01-2007, 03:37 AM
Ok I found this great code man I think it fits most your needs, besides it validates itself by checking file type, size, and you can even upload more than 1 file...
Heres the PHP code

<?php//upload directory.
//change to fit your need eg. files, upload .... etc.
$upload_dir = "images/";
//number of files to upload.
$num_files = 5;
//the file size in bytes.
$size_bytes =51200; //51200 bytes = 50KB.
//1024 divided by bytes gets you KB
//Extensions you want files uploaded limited to.
$limitedext = array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");


//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
}


//if the form has been submitted, then do the upload process
//infact, if you clicked on (Upload Now!) button.
if (isset($_POST['upload_form'])){

echo "<h3>Upload results:</h3>";

//do a loop for uploading files based on ($num_files) number of files.
for ($i = 1; $i <= $num_files; $i++) {

//define variables to hold the values.
$new_file = $_FILES['file'.$i];
$file_name = $new_file['name'];
//to remove spaces from file name we have to replace it with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_tmp = $new_file['tmp_name'];
$file_size = $new_file['size'];

#-----------------------------------------------------------#
# this code will check if the files was selected or not. #
#-----------------------------------------------------------#

if (!is_uploaded_file($file_tmp)) {
//print error message and file number.
echo "File $i: Not selected.<br>";
}else{
#-----------------------------------------------------------#
# this code will check file extension #
#-----------------------------------------------------------#

$ext = strrchr($file_name,'.');
if (!in_array(strtolower($ext),$limitedext)) {
echo "File $i: ($file_name) Wrong file extension. <br>";
}else{
#-----------------------------------------------------------#
# this code will check file size is correct #
#-----------------------------------------------------------#

if ($file_size > $size_bytes){
echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
}else{
#-----------------------------------------------------------#
# this code check if file is Already EXISTS. #
#-----------------------------------------------------------#

if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already exists.<br>";
}else{
#-----------------------------------------------------------#
# this function will upload the files. :) ;) cool #
#-----------------------------------------------------------#
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
echo "File $i: ($file_name) Uploaded.<br>";
}else{
echo "File $i: Faild to upload.<br>";
}#end of (move_uploaded_file).

}#end of (file_exists).

}#end of (file_size).

}#end of (limitedext).

}#end of (!is_uploaded_file).

}#end of (for loop).
# print back button.
echo "&#187;<a href=\"$_SERVER[PHP_SELF]\">back</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form didn't submitted then show it.
}else{
echo " <h3>Select files to upload!.</h3>
Max file size = ". $size_bytes / 1024 ." KB";
echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
// show the file input field based on($num_files).
for ($i = 1; $i <= $num_files; $i++) {
echo "File $i: <input type=\"file\" name=\"file". $i ."\"><br>";
}
echo " <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\">
<input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
</form>";
}

?>
Hopefully it works for you...
Note I got it off PHPFREAKS.com

WebMaker12
09-01-2007, 04:03 AM
Awesome guys!!!

Don't know what to say besides Thank you soooo much for your time.

Maybe you guys can make this a sticky or something incase anyone has a similar question, i think we covered every block to uploading anything. :)

Thanks a lot,

God Bless