Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding a title field to a very simple PHP script

    Hello all...

    I have this very simple script which allows for a user to upload any type of file to their server, then has a download action for site users to download.

    A love the ease of the script- its only 1 page, but currently when the script shows files available for download, it lists it by file name. I want to be able to list the files by a description or title field I would need to add in.

    Ex, right now if I uploaded a file called test.doc, the results page would display it as: test.doc. I would prefer it to display the link as what I ever I enter in the description field, ie: Test Document (which hyperlinks to test.doc for downloading).

    Is this possible??? I would really really appreciate anyone's help in assisting me to imlement this.

    THANKS in advance for your help.

    Jon

    Here's the 1 page script:

    <?
    $extlimit = "no"; //Do you want to limit the extensions of files uploaded
    $limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to.
    $sizelimit = "no"; //Do you want a size limit, yes or no?
    $sizebytes = "200000"; //size limit in bytes
    $dl = "http://www.ulnotes.com/testbank"; //url where files are uploaded
    $absolute_path = "...\www\\testbank"; //Absolute path to where files are uploaded
    $websiteurl = "http://www.test.com"; //Url to you website
    $websitename = "k";

    if (!isset($action)) {
    $action = "";
    }

    switch($action) {
    default:
    echo"
    <html>
    <head>
    <title>Upload Or Download</title>
    </head>
    <body>
    <a href=$PHP_SELF?action=upload>Upload File</a>
    <a href=$PHP_SELF?action=download>Download File</a>
    <a href=$websiteurl>Return to $websitename</a>
    <br><br>
    </body>
    </html>";
    break;
    case "download":
    echo "
    <html>
    <head>
    <title>File Download</title>
    </head>
    <body><a href=$PHP_SELF?action=upload>Upload File</a> <a href=$websiteurl>Return to $websitename</a>";
    $list = "<table width=700 border=1 bordercolor=#000000 style=\"border-collapse: collapse\">";
    $list .= "<tr><td width=700><center><b>Click To Download</b></center></td></tr>";
    $dir = opendir($absolute_path);
    while($file = readdir($dir)) {
    if (($file != "..") and ($file != ".")) {
    //Download files with spaces fix by Kokesh
    $list .= "<tr><td width=700><a href='$dl/$file'>$file</a></center></td></tr>";
    }
    }
    $list .= "</table>";
    echo $list;
    echo"
    <br><br>
    </body>
    </html>";
    break;

    case "upload":
    echo"
    <html>

    <head>
    <title>File Upload</title>
    </head>

    <body>

    <form method=POST action=$PHP_SELF?action=doupload enctype=multipart/form-data>
    <p>File to upload:<br>
    <input type=file name=file size=30>
    <p><button name=submit type=submit>
    Upload
    </button>
    </form>
    <br><br>
    </body>

    </html>";
    break;


    //File Upload
    case "doupload":
    $dir = "dir";
    if ($file != "") {

    if (file_exists("$absolute_path/$file_name")) {
    die("File already exists");
    }

    if (($sizelimit == "yes") && ($file_size > $sizebytes)) {
    die("File is to big. It must be $sizebytes bytes or less.");
    }

    $ext = strrchr($file_name,'.');
    if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
    die("The file you are uploading doesn't have the correct extension.");
    }

    @copy($file, "$absolute_path/$file_name") or die("The file you are trying to upload couldn't be copied to the server");

    } else {
    die("Must select file to upload");
    }
    echo "
    <html>
    <head>
    <title>File Uploaded</title>
    </head>
    <body>";
    echo $file_name." was uploaded";
    echo "<br>
    <a href=$PHP_SELF?action=upload>Upload Another File</a>
    <a href=$PHP_SELF?action=download> Download File</a>
    <a href=$websiteurl> Return to $websitename</a><br><br>
    </body>
    </html>";
    break;

    }
    ?>

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    This script doesn't use a database.

    It uploads the file into a directory then displays all the files in that directory for download.

    If you wanted to add the feature you are talking about you would need to have a mysql database or use a textfile.

    Do u have a mysql database?

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do have a mySQL database I could tie it into.

    I started thinking it was going to need a DB after I posted it.

    Would this hard to accomplish?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This code uses a direct upload to a directory. Though its not incorrect by the least, you cannot store a seperate name for it without a database, text file, cvs or xml file. If you do not want to use either of these methods, you would then need to change the filename to the stated name, instead of using the name supplied with the document. You could also look at simply appending the names together and using php to read it with an explode off of its type.
    Let us know what you have for availability for a database which would be far simpler, otherwise an external means will be nessessary.

    Edit:
    Damn, late reply, lol
    No it would be super simple to impliment this in.
    Also, I thought it would be worth mentioning, you are relying on two things as well as I look over this, one is that short tags are enabled, (which I believe is a default standard of php, so this is unlikely to cause problems), the other problem is that it relys on register_globals being on, which is off by default and will probably cause you problems. Cleaning up would be a breeze if your interested.
    Last edited by Fou-Lu; 06-09-2005 at 05:14 PM.

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    This code should work, you will need to use the database I have made a dump for below. Also, you will need to change the username/password in the code above (at the top) so it can connect to the database.


    PHP Code:
    <?

    $conn 
    =mysql_connect('localhost''username''password'); //change to suit your database
    mysql_select_db('filesdb'$conn);


    $extlimit "no"//Do you want to limit the extensions of files uploaded
    $limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to.
    $sizelimit "no"//Do you want a size limit, yes or no?
    $sizebytes "200000"//size limit in bytes
    $dl "http://www.ulnotes.com/testbank"//url where files are uploaded
    $absolute_path "...\www\\testbank"//Absolute path to where files are uploaded
    $websiteurl "http://www.test.com"//Url to you website
    $websitename "k";

    if (!isset(
    $action)) {
    $action "";
    }

    switch(
    $action) {
    default:
    echo
    "
    <html>
    <head>
    <title>Upload Or Download</title>
    </head>
    <body>
    <a href=$_SERVER['PHP_SELF']?action=upload>Upload File</a>
    <a href=$_SERVER['PHP_SELF']?action=download>Download File</a>
    <a href=$websiteurl>Return to $websitename</a>
    <br><br>
    </body>
    </html>"
    ;
    break;
    case 
    "download":

    $result =mysql_query('SELECT `name`,`src` FROM `files` ORDER BY `id`'$con) or die(mysql_error());
    echo 
    "
    <html>
    <head>
    <title>File Download</title>
    </head>
    <body><a href=$_SERVER['PHP_SELF']?action=upload>Upload File</a> <a href=$websiteurl>Return to $websitename</a>"
    ;
    $list "<table width=700 border=1 bordercolor=#000000 style=\"border-collapse: collapse\">";
    $list .= "<tr><td width=700><center><b>Click To Download</b></center></td></tr>";
    while(
    $row mysql_fetch_assoc($result)) {
    $list .= "<tr><td width=700><a href=\'$row['src']\'>$row['name']</a></center></td></tr>";
    }
    $list .= "</table>";
    echo 
    $list;
    echo
    "
    <br><br>
    </body>
    </html>"
    ;

    break;

    case 
    "upload":
    echo
    "
    <html>

    <head>
    <title>File Upload</title>
    </head>

    <body>

    <form method=POST action=$_SERVER['PHP_SELF']?action=doupload enctype=multipart/form-data>
    <p>File to upload:<br>
    <input type=file name=file size=30><br>
    <p>File to upload:<br>
    <input type=text name=name>
    <p><button name=submit type=submit>
    Upload
    </button>
    </form>
    <br><br>
    </body>

    </html>"
    ;
    break;


    //File Upload
    case "doupload":
    $dir "dir";
    if (
    $_FILES['file']['tmpname'] != "" && $_POST['name'] != "") {

    if (
    file_exists("$absolute_path/$_FILES['file']['name']")) {
    die(
    "File already exists");
    }

    if ((
    $sizelimit == "yes") && ($_FILES['file']['size'] > $sizebytes)) {
    die(
    "File is to big. It must be $sizebytes bytes or less.");
    }

    $ext strrchr($_FILES['file']['name'],'.');
    if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {
    die(
    "The file you are uploading doesn't have the correct extension.");
    }

    @
    copy($_FILES['file'], "$absolute_path/$_FILES['file']['name']") or die("The file you are trying to upload couldn't be copied to the server");

    $sql "INSERT INTO `files` (`id`,`name`,`src`) VALUES ('','$_POST['name']','$absolute_path/$_FILES['file']['name']');";
    mysql_query($sql) or die("Could not add file to database");

    } else {
    die(
    "Must select file to upload");
    }
    echo 
    "
    <html>
    <head>
    <title>File Uploaded</title>
    </head>
    <body>"
    ;
    echo 
    $file_name." was uploaded";
    echo 
    "<br>
    <a href=$_SERVER['PHP_SELF']?action=upload>Upload Another File</a>
    <a href=$_SERVER['PHP_SELF']?action=download> Download File</a>
    <a href=$websiteurl> Return to $websitename</a><br><br>
    </body>
    </html>"
    ;
    break;

    }
    mysql_close($con);
    ?>

    Database dump
    Code:
    CREATE DATABASE `filesdb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
    USE filesdb;
    
    CREATE TABLE `files` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(255) NOT NULL default '',
      `src` varchar(255) NOT NULL default '',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=1 ;

  • #6
    New Coder
    Join Date
    Jun 2005
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much for the reply! Sorry for the delay in responding... was on vacation.

    I installed the mysql table and tried to run the php, but returns this error:
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /client/home/matthewcurran/docs/html/untitled1.php on line 28

    Line 28: <a href=$_SERVER['PHP_SELF']?action=upload>Upload File</a>

    Does this mean anything to you or anyone?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •