A Php Upload Script?

Discussion in 'The Living Room' started by Hoodie, Jan 12, 2003.

  1. #1
    Hoodie

    Hoodie Well-Known Member

    Joined:
    Jan 12, 2003
    Messages:
    48
    Likes Received:
    0



    i heard there was a way to upload to directories from web pages through PHP...? I need to learn how to write a lil pup that will upload images for a photo gallery I'm coding... Is there a function that uploads? thanks for advice B)
     
  2. #2
    Alex

    Alex Ambient

    Joined:
    Jul 13, 2002
    Messages:
    1,552
    Likes Received:
    4



    im sure you can try something with this

    Code:
    <?php
    if ($HTTP_POST_VARS['submit']) {
    if (!is_uploaded_file($HTTP_POST_FILES['file']['tmp_name'])) {
    $error = "You did not select a file to upload";
    unlink($HTTP_POST_FILES['file']['tmp_name']);
    } else {
    //a file was uploaded do something
    $maxfilesize=10240;
    
    if ($HTTP_POST_FILES['file']['size'] > $maxfilesize) {
    $error = "The selected file is too large, it must be less
    than $maxfilesize bytes.";
    unlink($HTTP_POST_FILES['file']['tmp_name']);
    } else {
    //the file is under the specified number of bytes do
    something
    if($HTTP_POST_FILES['file']['type'] != "image/gif"
    AND $HTTP_POST_FILES['file']['type'] != "image/pjpeg" AND
    $HTTP_POST_FILES['file']['type'] !="image/jpeg") {
    $error = "This file type is not allowed";
    unlink($HTTP_POST_FILES['file']['tmp_name']);
    } else {
    //the file is the correct format do something
    
    copy($HTTP_POST_FILES['file']['tmp_name'],"/finallocation/".$HTTP_POST_FILES
    ['file']['name']);
    print "Your file has been successfully uploaded
    to /finallocation/".$HTTP_POST_FILES['file']['name'];
    exit;
    } 
    } 
    }
    }
    
    <html>
    <head></head>
    <body>
    <form action="<?=$PHP_SELF?>" method="post"
    enctype="multipart/form-data">
    <?=$error?>
    <br><br>
    Choose a file to upload:<br>
    <input type="file" name="file"><br>
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>
    
     
  3. #3
    Hoodie

    Hoodie Well-Known Member

    Joined:
    Jan 12, 2003
    Messages:
    48
    Likes Received:
    0



    :blink: wow, did you just write that? I'm gonna study that cuase I want to see how it works too, not just copy and paste it. Thanks Alex! B)
     
  4. #4
    Hoodie

    Hoodie Well-Known Member

    Joined:
    Jan 12, 2003
    Messages:
    48
    Likes Received:
    0



    Might I ask what a "!" represents in PHP?
     

Share This Page