SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    image upload help

    I am trying to create an image uploading script. I found an old code and did some editting to it but I can get it to work. This is the code I am using:

    PHP Code:
    <?php 
    if(isset( $_POST['$Submit'] )){ 
      
    $key rand(100999);
      
    $file=$_FILES['imagefile']['name']; 
      
    $filetype=substr($file,-4); 
      if((
    $filetype=="jpeg") || ($filetype==".jpg") || ($filetype==".gif") || ($filetype==".png") || ){ 
        
    copy($_FILES['imagefile']['tmp_name'],"useruploads/".$key.$_FILES['imagefile']['name']);
        or die(
    "Could not copy"); 
        echo
    "<br>Upload Complete"
        echo
    "<br>Name:&nbsp;".$_FILES['imagefile']['name'].""
        echo
    "<br>Size:&nbsp;".$_FILES['imagefile']['size'].""
        echo
    "<br>Type:&nbsp;".$_FILES['imagefile']['type']."<br>";  
      }else{ 
        echo
    "<br><span class="subtitle">Upload Error</span>"
        echo
    "<br>Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")<br>"
      } 

    ?>
    The parse error I recieved is:

    Parse error: parse error, unexpected ')' in /home/e39m5/public_html/newentry.php on line 11
    e39m5

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(($filetype=="jpeg") || ($filetype==".jpg") || ($filetype==".gif") || ($filetype==".png")){ 
    you had a || too much at the end.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Grrr, stupid mistake. Now I am getting a
    Parse error: parse error, unexpected T_LOGICAL_OR in /home/e39m5/public_html/newentry.php on line 13
    I believe the error is in this line:
    PHP Code:
    copy($_FILES['imagefile']['tmp_name'],"useruploads/".$key.$_FILES['imagefile']['name']); 
    I want the path to be useruploads/keyfilename.ext

    key shouuld be the randomly generated number and filename.ext is the file chosen to be uploaded.

    Thanks,
    e39m5

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    get rid of the semicolon ; after copy if you want to use a 'or die' after it.
    PHP Code:

    copy
    ($_FILES['imagefile']['tmp_name'],"useruploads/".$key.$_FILES['imagefile']['name'])
        or die(
    "Could not copy"); 
    im curious, do you really have an html field named $Submit, with the $???

    Code:
    <input type="submit" name="$Submit">

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks and nice catch. That may have saved me another question. The last mistake in that file was I needed backslashes in the second to last echo.

    e39m5

  6. #6
    SitePoint Member Catalin Ene's Avatar
    Join Date
    Aug 2006
    Location
    Romania Galati
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this : file name is upload.php

    <?php

    $action = $_POST["action"];
    if ($action == "add") {

    $key = rand(100, 999);
    $file_name=$HTTP_POST_FILES['imagefile']['name'];
    $file_size=$HTTP_POST_FILES['imagefile']['size'];
    $file = $HTTP_POST_FILES['imagefile']['tmp_name'];

    $filetype=substr($file_name,-4);
    if(($filetype=="jpeg") || ($filetype==".jpg") || ($filetype==".gif") || ($filetype==".png") )

    {

    if(!copy($file,"useruploads/".$file_name)) {
    echo "Could not copy";
    } else {
    echo"<br>Upload Complete";
    echo"<br>Name:&nbsp;".$file_name."";
    echo"<br>Size:&nbsp;".$file_size."";
    echo"<br>Type:&nbsp;".$filetype."<br>";
    } //eof else


    }else{
    echo"<br><span class=subtitle>Upload Error</span>";
    echo"<br>Could Not Copy, Wrong Filetype (".$file_name.")<br>";
    }
    }
    ?>

    <form action="upload.php" method="POST" name="add" enctype="multipart/form-data">
    <input type="hidden" name="action" value="">
    <input type="file" name="imagefile" value="">
    <INPUT TYPE="button" onclick="javascript: document.add.action.value='add';
    document.add.submit();" value = "UPLOAD">

    </form>

  7. #7
    SitePoint Member Catalin Ene's Avatar
    Join Date
    Aug 2006
    Location
    Romania Galati
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    enctype="multipart/form-data" is important

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, you should use move_uploaded_file() instead of copy.

    see the manual.
    http://www.php.net/manual/en/features.file-upload.php

  9. #9
    SitePoint Member Catalin Ene's Avatar
    Join Date
    Aug 2006
    Location
    Romania Galati
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!

  10. #10
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the extensive help on this. To catalin, are there any signifcant advatages to your code compared to mine? (security maybe?). Also, what does enctype="multipart/form-data" do? I tryed doing some research into it but only found people with questions about it.

    I have changed my code so now it used move uploaded file rather than copy. I did reasearch into it and all sources seem to say move uploaded file is better than copy. thanks clamcrusher

    Thanks,
    e39m5

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  12. #12
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    netherlands
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Does anyone has an example for these add's ?
    I would like to let the user upload multiple files to a specific directory, is that possible ? Like this:
    Maindir = Matches
    Subdirs = match1, match2,..etc
    Target Dir = Maindir\Subdirs\pics
    He should be able to browse ( a part maybe?) of the directory structure.

    Thanks
    MySQL version is 4.1.10


Bookmarks

Posting Permissions

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