ummmm upload help please... might be in the wrong section

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xxgraysonxx
Forum Newbie
Posts: 2
Joined: Thu Nov 25, 2010 8:03 pm

ummmm upload help please... might be in the wrong section

Post by xxgraysonxx »

.... ok first off this might be in the wrong section... if so admin please move... second...

im looking to add an upload function to my website.... im looking for something simple that i dont have to code for myself...

i want a simple html page that has an browse and upload button that u select any file type and hit upload....

so example


upload website

http://www.mysite.com/uploader.html > browse > upload > http://www.mysite.com/upload1.html


on upload1.html i would like it to say congratulations your file has been uploaded adress is http://www.mysite.com/uploads/ioajsflk.file

and preferably when the file is uploaded it want it to hold the same name and if there is a file with a similar name to it just add a 2,3,4 etc. at the end....

i have ftp access to my website so i can do it that way...


in all... sorry if this is confusing im not on my computer and just trying to add this function but everything ive found is a bunch of php and stuff that i dont know and i keep running into dead ends....
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ummmm upload help please... might be in the wrong sectio

Post by Celauran »

So something like this?

Code: Select all

<?php

$basedir = "http://www.yoursite.com";

if ($_FILES)
{
    $file = basename($_FILES['uploadedfile']['name']);
    $path = 'uploads/' . $file;
    $ext = substr($file, strrpos($file, '.'));
    $i = 1;
    while (file_exists($path))
    {
        if ($i == 1)
        {
            $path = substr($path, 0, (strlen($path) - strlen($ext))) . $i . $ext;
        }
        else
        {
            $path = substr($path, 0, (strlen($path) - strlen($ext) - 1)) . $i . $ext;
        }
        $i++;
    }
    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path))
    {
        $upload_path = htmlspecialchars(str_replace(' ', '%20', $path));
        echo "The file has been uploaded and can be found at 
              <a href=\"$basedir/$upload_path\">
              $basedir/$upload_path</a>";
    }
    else
    {
        echo "There was an error uploading the file";
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>File Upload</title>
    </head>
    <body>
        <form enctype="multipart/form-data" action="" method="post">
            Choose a file:  <input type="file" name="uploadedfile" />
            <input type="submit" value="Upload" />
        </form>
    </body>
</html>
Note this was thrown together in a few minutes and so hasn't implemented file size limits, extension checking (eg. to forbid upload of .exe files), etc. Should be enough to get you started, though. Some recommended reading to help you build on it.
xxgraysonxx
Forum Newbie
Posts: 2
Joined: Thu Nov 25, 2010 8:03 pm

Re: ummmm upload help please... might be in the wrong sectio

Post by xxgraysonxx »

ok the code is nice and clean... the only problem is i tried uploading a test file just a small .exe and it never showed up in the server....
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ummmm upload help please... might be in the wrong sectio

Post by Celauran »

Does the uploads subdirectory exist? Is it writeable? The code I posted is tested and works.
Post Reply