www.webdeveloper.com
Results 1 to 13 of 13

Thread: PHP Upload Script

  1. #1
    Join Date
    Nov 2009
    Posts
    9

    PHP Upload Script

    I have been following a video tutorial on how to make an image upload website, but he hasn't finished it yet and I went to get it done tonight.
    index.php code:
    PHP Code:
    <html>
    <
    form action="upload.php" method="post" enctype="multipart/form-data">
        <
    input type="file" name="myfile"><p>
        <
    input type="submit" value="Upload">
    </
    form>

    </
    html
    upload.php code:
    PHP Code:
    <?php

    // properties of the uploaded file
    $name $_FILES["myfile"]["name"];
    $type $_FILES["myfile"]["type"];
    $size $_FILES["myfile"]["size"];
    $temp $_FILES["myfile"]["tmp_name"];
    $error $_FILES["myfile"]["error"];

    if (
    $error 0)
        die(
    "Error uploading file! Code $error.");
    else
    {
        
    if(
    $type "video/avi"//conditions for the file
    {
      die(
    "That format is not allowed");    
    }
    else
    {
     
    move_uploaded_file($temp,"uploaded/".$name);
     echo 
    "Upload complete";
     }
    }

    ?>

    At the moment the file you upload goes into the uploaded folder, but it does not show the user who uploaded the file the image.

    You can view a live example here:
    http://cure2boredom.x10hosting.com/SpeedyUpload/

    Thanks for the help

  2. #2
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    34
    Try this...

    PHP Code:
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {    
     if(
    $type "video/avi") { 
      die(
    "That format is not allowed");     
     } else  { 
      if(
    move_uploaded_file($temp,"uploaded/".$name)) { 
        echo 
    "Upload complete"
      } else {
        echo 
    "Failed to complete the upload";
      }
     }  

    Hopefully that solves the issue for you.

  3. #3
    Join Date
    Nov 2009
    Posts
    9
    Now it is saying That format is not allowed, which I am uploading a .png
    Last night it wasn't doing that

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    if($type = "video/avi") {
    Should be:
    PHP Code:
    if($type == "video/avi") { 

  5. #5
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    34
    Remove the type checker so you can upload anything as follows

    PHP Code:
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {    
      if(
    move_uploaded_file($temp,"uploaded/".$name)) { 
        echo 
    "Upload complete"
      } else {
        echo 
    "Failed to complete the upload";
      }


  6. #6
    Join Date
    Nov 2009
    Posts
    9
    Quote Originally Posted by jassinc View Post
    Remove the type checker so you can upload anything as follows

    PHP Code:
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {    
      if(
    move_uploaded_file($temp,"uploaded/".$name)) { 
        echo 
    "Upload complete"
      } else {
        echo 
    "Failed to complete the upload";
      }

    used that and it fixed the problem, but it still doesnt show the uploader the picture, it just shows "Upload Complete" which is should, but I want the image also. It shows the image in the "uploaded" folder when I view it in FileZilla.

    live example:
    http://cure2boredom.x10hosting.com/S...oad/upload.php


    Thanks

  7. #7
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    34
    You'll need this...

    PHP Code:
    // properties of the uploaded file 
    $name = $_FILES["myfile"]["name"]; 
    $type = $_FILES["myfile"]["type"]; 
    $size = $_FILES["myfile"]["size"]; 
    $temp = $_FILES["myfile"]["tmp_name"]; 
    $error = $_FILES["myfile"]["error"]; 

    if ($error > 0) {
        die("Error uploading file! Code $error."); 
    } else  {    
      if(move_uploaded_file($temp,"uploaded/".$name)) { 
        ?>
         <!-- Start Success HTML -->
         Upload complete <br/><br/>
         <img src="uploaded/<? echo $name?>" alt="Uploaded Image">
         <!-- End Success HTML -->
        <? 
      
    } else {
        echo 
    "Failed to complete the upload";
      }
    }
    You can strip out and HTML you don't want (or add some) in between where I've commented it.

  8. #8
    Join Date
    Nov 2009
    Posts
    9
    It works now, and the image uploads, but I would like it to upload the image to a randomized URL that someone can copy and paste.

  9. #9
    Join Date
    Nov 2009
    Posts
    9
    Sorry for the double post, but I would like it like this
    http://www.picrange.com/images/klz1259014403d.JPG

    The klz1259014403d.JPG being the image

  10. #10
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    34
    PHP Code:
    <?
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {   
      
    $ext strtolower(substr($namestrrpos($name"."))); // Get Extension
      
    $newname time().rand(100,999).$ext// Generate Random File Name
      
    if(move_uploaded_file($temp,"uploaded/".$newname)) { 
        
    ?>
         <!-- Start Success HTML -->
         Upload complete <br/><br/>
         <img src="uploaded/<? echo $newname?>" alt="Uploaded Image">
         <!-- End Success HTML -->
        <? 
      
    } else {
        echo 
    "Failed to complete the upload";
      }
    }
    ?>

  11. #11
    Join Date
    Nov 2009
    Posts
    9
    Quote Originally Posted by jassinc View Post
    PHP Code:
    <?
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {   
      
    $ext strtolower(substr($namestrrpos($name"."))); // Get Extension
      
    $newname time().rand(100,999).$ext// Generate Random File Name
      
    if(move_uploaded_file($temp,"uploaded/".$newname)) { 
        
    ?>
         <!-- Start Success HTML -->
         Upload complete <br/><br/>
         <img src="uploaded/<? echo $newname?>" alt="Uploaded Image">
         <!-- End Success HTML -->
        <? 
      
    } else {
        echo 
    "Failed to complete the upload";
      }
    }
    ?>
    That didn't generate the random URL.
    Upload.php code below:

    PHP Code:
    <?php
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {   
      
    $ext strtolower(substr($namestrrpos($name"."))); // Get Extension
      
    $newname time().rand(100,999).$ext// Generate Random File Name
      
    if(move_uploaded_file($temp,"uploaded/".$newname)) { 
        
    ?>
         <!-- Start Success HTML -->
         Upload complete <br/><br/>
         <img src="uploaded/<? echo $newname?>" alt="Uploaded Image">
         <!-- End Success HTML -->
        <? 
      
    } else {
        echo 
    "Failed to complete the upload";
      }
    }
    ?>

  12. #12
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    34
    I'd strongly suggest you learn the basics of PHP so you understand what is happening and can modify the script accordingly.

    Go to Tizag.com and run through the php tutorials as you really should be able to read the code and have it read out the address. If you can't do that then you shouldn't be delving into PHP.

  13. #13
    Join Date
    Nov 2009
    Posts
    9
    Quote Originally Posted by jassinc View Post
    PHP Code:
    <?
    // properties of the uploaded file 
    $name $_FILES["myfile"]["name"]; 
    $type $_FILES["myfile"]["type"]; 
    $size $_FILES["myfile"]["size"]; 
    $temp $_FILES["myfile"]["tmp_name"]; 
    $error $_FILES["myfile"]["error"]; 

    if (
    $error 0) {
        die(
    "Error uploading file! Code $error."); 
    } else  {   
      
    $ext strtolower(substr($namestrrpos($name"."))); // Get Extension
      
    $newname time().rand(100,999).$ext// Generate Random File Name
      
    if(move_uploaded_file($temp,"uploaded/".$newname)) { 
        
    ?>
         <!-- Start Success HTML -->
         Upload complete <br/><br/>
         <img src="uploaded/<? echo $newname?>" alt="Uploaded Image">
         <!-- End Success HTML -->
        <? 
      
    } else {
        echo 
    "Failed to complete the upload";
      }
    }
    ?>
    Quote Originally Posted by jassinc View Post
    I'd strongly suggest you learn the basics of PHP so you understand what is happening and can modify the script accordingly.

    Go to Tizag.com and run through the php tutorials as you really should be able to read the code and have it read out the address. If you can't do that then you shouldn't be delving into PHP.
    Thanks but I got it to work, it was something I didn't get but with a little tweaking it works how I want it to now.

    Thanks all for the help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles