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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts

    why can't upload image

    PHP Code:
    <?php

    $allowed_filetypes 
    = array('.jpg','.gif','.bmp','.png');

    $max_filesize 5242888;  

    $upload_path '/files';

    $filename =$_FILES['userfile']['name'];

    $ext substr($filename,strpos($filename,'.'),strlen($filename)-1);//get the extension form the filename

    if(!in_array($ext,$allowed_filetypes))
        die(
    'the file you attempted to upload is not allowed.');

    if(
    filesize($_FILES['userfile']['size'])>$max_filesize)
      die(
    'the file you attempted to upload is too large.');
      

      if(!
    is_writable($upload_path)){
        die(
    'you cannot upload to the specified directory,please CHMOD it to 777.');
      }
      
      
      if(
    move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path.$filename)){
        echo 
    'you file upload successful.view the file <a href=".$upload_path.$filename.title="your file">here</a>';
      }
      else{
        echo 
    'failed';
      }
    the code is the upload.php,what's wrong with it. it can't upload a jpg image.

    the main html

    Code:
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="userfile" id="file"/>
    <button>upload</button>
    Last edited by runeveryday; 12-07-2010 at 07:49 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    <form action="upload.php" method="post">
    You need to specify the enctype attribute of the form, with the required value, read http://htmldog.com/reference/htmltags/form/
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    You need to specify the enctype attribute of the form, with the required value, read http://htmldog.com/reference/htmltags/form/
    i have changed it to "<form action="upload.php" method="post" enctype="multipart/form-data">"
    but it still can't work.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Add
    PHP Code:
    echo '<pre>';
    print_r($_FILES);
    echo 
    '</pre>'
    at the top and check the output.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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