Jump to content

I need help with php File Upload script


AS

Recommended Posts

Here is the code I am using:

________The form

<form action="upload_file.php" method="post" enctype="multipart/form-data">

<label for="file">Select Filename:</label> <input type="file" name="file" id="file" />

 

<input type="submit" name="submit" value="Upload File" /> </form>

 

-----The upload_file.php

<?php $target_path = "upload/";

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

$uploadFile = $target_path . $_FILES['file']['name'];

move_uploaded_file($_FILES['file']['tm… $target_path);

if(move_uploaded_file($_FILES['file'][… $target_path)) {

echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";

} else{

echo "There was an error uploading the file, please try again!";} ?>

______________________________________…

 

Whenever I upload a file I get my error message "There was an error uploading the file, please try again!". I don't seem to see what's wrong with it so I thought anyone could help. Thanks

Link to comment
https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/
Share on other sites

check the file permissions on the folder you are uploading the image into. if php can't write/save to this directory it will fail.

 

in your ftp client, get info on the folder and give that folder full permissions (777) and see if that changes anything.

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" />
Choose a file to upload: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit_x" value="Upload File" />
</form>
<?php
//Working for uploading the files of a particular type
if(isset($_POST['submit_x']))
{
       function file_upload_error_message($error_code)
{
    switch ($error_code)
    {
        case 0:
            return 'There is no error, the file uploaded with success...';
        case 1:
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
        case 2:
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
        case 3:
            return 'The uploaded file was only partially uploaded';
        case 4:
            return 'No file was uploaded';
        case 6:
            return 'Missing a temporary folder';
        case 7:
            return 'Failed to write file to disk';
        case 8:
            return 'File upload stopped by extension';
        default:
            return 'Unknown upload error';
    }
}

    $host="localhost";
    $user="root";
    $pass="admin";
    $db="develop";
    mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());
    $query="select type_name from filetype";
    $result=mysql_query($query);
    ini_set("display_errors",1);
    //error_reporting(E_ALL);
    $target = "upload/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1));
    print_r($_FILES['uploaded']);
    $error_code=$_FILES['uploaded']['error'];
       $error_message = file_upload_error_message($error_code);
    echo "Name ".$_FILES['uploaded']['name']."<br>";
    echo "Type ".$_FILES['uploaded']['type']."<br>";
    echo "Error ".$error_message."<br>";
    echo "Size ".$_FILES['uploaded']['size']."<br>";


    //echo "<br>FileX ".$uploaded_type;
     while($row = mysql_fetch_array($result))
     {
          if($uploaded_type==$row['type_name'])
          {

              if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
              {
              echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
                  break;
              }
              else
              {
              echo "Sorry, there was a problem uploading your file.";
              }

          }
     }
}
?>

 

try this upload script and also check the permissions of the folder upload to be 777,

this script will surely upload the files..

u need to have a mysql connecting for this to work.

the table structure and the data that need to b there is like this..

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `develop`;

/*Table structure for table `filetype` */

DROP TABLE IF EXISTS `filetype`;

CREATE TABLE `filetype` (
  `type_name` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`type_name`)
) TYPE=MyISAM;

/*Data for the table `filetype` */

insert  into `filetype`(`type_name`) values ('doc'),('gif'),('jpg'),('pdf'),('png');

  • 1 month later...

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" />
Choose a file to upload: <input name="uploaded" type="file" /><br />
<input type="submit" name="submit_x" value="Upload File" />
</form>
<?php
//Working for uploading the files of a particular type
if(isset($_POST['submit_x']))
{
       function file_upload_error_message($error_code)
{
    switch ($error_code)
    {
        case 0:
            return 'There is no error, the file uploaded with success...';
        case 1:
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
        case 2:
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
        case 3:
            return 'The uploaded file was only partially uploaded';
        case 4:
            return 'No file was uploaded';
        case 6:
            return 'Missing a temporary folder';
        case 7:
            return 'Failed to write file to disk';
        case 8:
            return 'File upload stopped by extension';
        default:
            return 'Unknown upload error';
    }
}

    $host="localhost";
    $user="root";
    $pass="admin";
    $db="develop";
    mysql_connect($host,$user,$pass) or die(mysql_error());
    mysql_select_db($db) or die(mysql_error());
    $query="select type_name from filetype";
    $result=mysql_query($query);
    ini_set("display_errors",1);
    //error_reporting(E_ALL);
    $target = "upload/";
    $target = $target . basename( $_FILES['uploaded']['name']) ;
    $uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1));
    print_r($_FILES['uploaded']);
    $error_code=$_FILES['uploaded']['error'];
       $error_message = file_upload_error_message($error_code);
    echo "Name ".$_FILES['uploaded']['name']."<br>";
    echo "Type ".$_FILES['uploaded']['type']."<br>";
    echo "Error ".$error_message."<br>";
    echo "Size ".$_FILES['uploaded']['size']."<br>";


    //echo "<br>FileX ".$uploaded_type;
     while($row = mysql_fetch_array($result))
     {
          if($uploaded_type==$row['type_name'])
          {

              if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
              {
              echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
                  break;
              }
              else
              {
              echo "Sorry, there was a problem uploading your file.";
              }

          }
     }
}
?>

 

try this upload script and also check the permissions of the folder upload to be 777,

this script will surely upload the files..

u need to have a mysql connecting for this to work.

the table structure and the data that need to b there is like this..

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `develop`;

/*Table structure for table `filetype` */

DROP TABLE IF EXISTS `filetype`;

CREATE TABLE `filetype` (
  `type_name` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`type_name`)
) TYPE=MyISAM;

/*Data for the table `filetype` */

insert  into `filetype`(`type_name`) values ('doc'),('gif'),('jpg'),('pdf'),('png');

 

 

ym_chaitu,

I tried your sample code, and it seems to be working,  the image is uploading.

I'm just curious about it though, maybe this code isn't complete.  How does the filesize, and extensions get checked before they are uploaded?  I don't see anything in the code,  just in the form about the value of the file size.  But I don't see anything being done on the server.

 

 

 

here i am checking for the file type

$uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1));

 

and here i am comparing the type..

 if($uploaded_type==$row['type_name'])

 

the file size is here i am checking out..

 

<input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" />

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.