0

hi i am having a script which allows user to upload images but it is not uploading the images. everything is fine like rand number etc but the image is not only being uploaded. Following is my image uploading form.

    <form action="register.php" method="post" enctype="multipart/form-data" name="regForm" id="regForm" >
        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
           <tr>
               <td>Profile Image<span class="required"><font color="#CC0000">*</font></span> </td>
               <td><input name="user_image" type="file" class="required password" id="user_image"> 
                   <span class="example">Upload your image</span>
                   <input name="doRegister" type="submit" id="doRegister" value="Register">
               </td>
           </tr>
        </table>
    </form>

And this is register.php

<?php
 $path = "user/".time().uniqid(rand()).basename($_FILES['user_image']['name'],'.');
 if($user_image !=none)
 {
      move_uploaded_file($_files['user_image']['tmp_name'], $path);
      {
          echo "Successful<BR/>"; 
          echo "File Name :".$HTTP_POST_FILES['user_image']['name']."<BR/>"; 
          echo "File Size :".$HTTP_POST_FILES['user_image']['size']."<BR/>"; 
          echo "File Type :".$HTTP_POST_FILES['user_image']['type']."<BR/>"; 
          echo "<img src=\"$path\" width=\"150\" height=\"150\">";
      }
}
else
{
   echo "Error";
}
?>

In this my uploading folder is user and i also want to know that what is the file name after being uploaded as i want to show it to users so how can I do so. Thanks in advance! If you need more information ask me.

5
  • the html form looks ok but the PHP script looks confusing. for example, where is the '$user_image' variable defined?, what is the meaning of the constant 'none'? Commented Oct 3, 2012 at 7:39
  • $http_post_files is deprecated use $FILES['user_image']['name'] instead Commented Oct 3, 2012 at 7:40
  • Are you getting any errors returned? Does your upload location have proper permissions (chmod 755 or 777) set? Commented Oct 3, 2012 at 7:44
  • just try to follow the steps as mention in following links w3schools.com/php/php_file_upload.asp Commented Oct 3, 2012 at 7:46
  • @darshan.dodiya hey i got the script working from there but i also want to rename its name can you tell me how to do so? Commented Oct 3, 2012 at 9:18

2 Answers 2

1

HTML form do as usual,

Below the coding will automatically create a new folder number based on generation, inside the folder there it will consist the upload. If you want to change the path of the upload, Use \ INSTEAD of / because it will cause error.

Register.php

 if ($_POST['doRegister'] == "Register")
{

    $path1 = "C:\Uploads\ ";
    if (file_exists($path1))
    {
    $path = $path1 .time().uniqid(rand()).'\ ';


$target_path = $path . basename( $_FILES['user_image']['name']);

if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target_path)) {
    echo "Successfully uploaded on $path".$_FILES['user_image']['name']."<br>";

    echo "File Name :".$_FILES['user_image']['name']."<BR/>"; 
          echo "File Size :".$_FILES['user_image']['size']."<BR/>"; 
          echo "File Type :".$_FILES['user_image']['type']."<BR/>"; 
}
    }
else
{

    mkdir($path1);
    $path = $path1 .time().uniqid(rand()).'\ ';
mkdir($path);

$target_path = $path . basename( $_FILES['user_image']['name']);

if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target_path)) {
    echo "Successfully uploaded on $path".$_FILES['user_image']['name']."<br>";

    echo "File Name :".$_FILES['user_image']['name']."<BR/>"; 
          echo "File Size :".$_FILES['user_image']['size']."<BR/>"; 
          echo "File Type :".$_FILES['user_image']['type']."<BR/>"; 
}

}   

}
?>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php


if(isset($_FILES['user_image']))
{ 
 $path = "user/".time().uniqid(rand()).basename($_FILES['user_image']['name'],'.');

  move_uploaded_file($_files['user_image']['tmp_name'], $path);
  {
      echo "Successful<BR/>"; 
      echo "File Name :".$HTTP_POST_FILES['user_image']['name']."<BR/>"; 
      echo "File Size :".$HTTP_POST_FILES['user_image']['size']."<BR/>"; 
      echo "File Type :".$HTTP_POST_FILES['user_image']['type']."<BR/>"; 
      echo "<img src=\"$path\" width=\"150\" height=\"150\">";
  }
}
else
{
  echo "No File Chosen";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.