tags:

views:

109

answers:

8

Here is my code;

<?php

 define ("MAX_SIZE","500"); 

 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;
 if(isset($_POST['Submit'])) 
 {

    $image=$_FILES['image']['name'];
    if ($image) 
    {
     $filename = stripslashes($_FILES['image']['name']);

     $extension = getExtension($filename);
     $extension = strtolower($extension);

 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
     {
     //print error message
      echo '<h1>Unknown extension!</h1>';
      $errors=1;
     }
     else
     {

 $size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1024)
{
    echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
}

$image_name=time().'.'.$extension;
$newname="user_img/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
    echo '<h1>Copy unsuccessful!</h1>';
    $errors=1;
}}}}

 if(isset($_POST['Submit']) && !$errors) 
 {
    echo "<h1>File Uploaded Successfully! Try again!</h1>";
 }

?>

This is working fine at my localhost but when i uploaded it on server it wont works at all and not showing any error as well just showing message of unsuccessful. Whats could be the problem and solution Thanks

+2  A: 

you prolly need to chmod the folder you are uploading to in order to get the proper permissions

You can do this very easy with FileZilla.

Natrium
Thanks. It was permission problem.
Talha Bin Shakir
what's with the downvote?
Natrium
Sorry i wrongly clicked. pls edit your answer so i can vote again.
Talha Bin Shakir
A: 

Check that the user your script is running as on your host has permission to write to the user_img directory.

Ian Kemp
A: 

There could be several issues:

  • at first make sure you have error_reporting enabled, to see errors if they appear
  • check you php settings: file_uploads, upload_max_filesize (description)
  • check the permissions for the folder you want to write the files
  • also try move_uploaded_file function
Hippo
A: 

This is likely a permissions error, make sure you have adequate permissions for the user that php runs on in the directory that you writing the images. Another possibility is that you are exceeding the maximum POST size that is set in your php.ini file.

Alex Sexton
A: 

Try move_uploaded_file instead of copy. Also look at this article to find better ways of handling image file uploads.

Salman A
A: 

You might also consider using the absolute path to your user_img folder which would alleviate any issues regarding the actual relative path to the folder:

$newname="user_img/".$image_name;

switches to:

$newname="/path/to/www/user_img/".$image_name;
cballou
A: 

Apart from checking file permission:

  1. I've always used move_uploaded_file() rather than copy()
  2. Your destination file is "user_img/".$image_name - is that relative directory valid on your server as well as localhost? You may want to convert to a fully qualified path
Steve Claridge
A: 

see this example for simple image upload: http://kedar.nitty-witty.com/blog/upload-image-to-mysql-using-php/

kedar