Jump to content

PHP Upload problem


7awaka

Recommended Posts

Hi

 

I have been trying for some time to upload files either to a mysql database or to a directory.

 

I have failed at both.

in the case of uploading to a database i have successfully uploaded the size and the name of the files to the db tables but no content.

 

in the case of uploading files to a directory, the code is fine but no files are copied to the directory location for some reason although i got a success message.

 

heres the  code:

 

the first page is the upload page

 

entry.php

<?php
echo'
      <form action="File.php" Method="post" enctype="multipart/form-data">
      <input type="hidden" name="upload" value="100000">
     <center> <input name="file"  type="file" id="file" size="20">
      <input type="submit"  value="upload">
      </form>   ';
?>

 

 

The second is ofcourse the page that takes the variables and actually uploads the file

File.php:-

<?php
require($_SERVER["DOCUMENT_ROOT"]. "/config/cv_config.php");
$connection = mysql_connect($db_host,$db_user,$db_password) or die ("error connecting") ;
mysql_select_db($db_name, $connection);

if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)
{
$fileName = $_FILES['file']['name'];
$tmpName  = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$directory= '/upload';
$path = "$directory/$fileName" ;

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));

fclose($fp);

move_uploaded_file($tmp_name, $path);
if(!move_uploaded_file($tmpName, $path))
{
echo 'Successful<BR/><a href='.$path.'> '.$path.' </a>';

}
else
{
echo 'error<a href='.$tmpName.'> '.$tmpName.' </a>';
}



if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
} 

       $query1 = "SELECT * FROM upload";
       $result = mysql_query($query1, $connection);


    echo '

       <table bgcolor="black" border="2" width="75%" cellspacing="1" cellpading="2" valign="top" >


                          <tr>
                          <td width="1%" valign="to[" nowrap bgcolor="Blue" >
                      <b><font face="arial" size="2.5" color="white"><center>
                          Name  </td>
                    <td width="10%" valign="to["wrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
                        .$fileName.'
                          </td>
                          </tr>


                          <tr>
                          <td width="1%" valign="to[" nowrap bgcolor="Blue">
                      <b><font face="arial" size="2.5" color="white"><center>
                          Size   </td>
                    <td width="10%" valign="to["nowrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
                          .$fileSize.'
                          </td>
                          </tr>



                          <tr>
                            <td width="1%" valign="to[" nowrap bgcolor="Blue">
                      <b><font face="arial" size="2.5" color="white"><center>
                          Type
                    <td width="10%" valign="to["wrap bgcolor="#EEEEEE"><b><font face="Comic Sans" size="2">'
                          .$fileType.'

                       </td>

                       </tr>

<tr>
                          <td width="1%" valign="to[" nowrap bgcolor="Blue">
                      <b><font face="arial" size="2.5" color="white"><center>
                          Content   </td>
                    <td width="10%" valign="to["nowrap bgcolor="#EEEEEE"><b><font  face="Comic Sans" size="2">'
                          .$content.'
                          </td>
                          </tr>

</table>';
?>

 

I get a successful message plus the directory path in hyperlink which i press on to get a 404 error page

and below it is the table that states the name,type,size and content of the file that is uploaded...all present but the content.

Why isnt it working....HELP!

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/130159-php-upload-problem/
Share on other sites

try this:

 

<?php
require($_SERVER["DOCUMENT_ROOT"]. "/config/cv_config.php");
$connection = mysql_connect($db_host,$db_user,$db_password) or die ("error connecting") ;
mysql_select_db($db_name, $connection);

if(isset($_POST['upload']))
{
  //Check for upload Error
  $uploadErrors = array(
    UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
    UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
    UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
    UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
    UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
    UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
    UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
  );
  if(!$_FILES['file'])
  {
    die("No file found, maybe it's too big?");
  }
  if($_FILES['file']['error'] !== UPLOAD_ERR_OK)
  {
    die(sprintf('Upload Error: %s',$uploadErrors[$_FILES['file']['error']]));
  }    

  $fileName = $_FILES['file']['name'];
  $tmpName  = $_FILES['file']['tmp_name'];
  $fileSize = $_FILES['file']['size'];
  $fileType = $_FILES['file']['type'];
  $directory= '/upload';
  $path = $directory.'/'.$fileName;

  if(!move_uploaded_file($tmp_name, $path))
  {
    die('Error moving file');
  }

  echo 'Successful<BR/><a href='.$path.'> '.$path.' </a>';
  if(get_magic_quotes_gpc())
  {
    $fileName = stripslashes($fileName);
  }


  include 'library/config.php';
  include 'library/opendb.php';
  $query = sprintf(
    "INSERT INTO upload (name, size, type, content ) VALUES ( '%s', '%s', '%s', '%s' )",
    mysql_real_escape_string($fileName),
    mysql_real_escape_string($fileSize),
    mysql_real_escape_string($fileType),
    mysql_real_escape_string($content)
  );
  mysql_query($query) or die('Error, query failed'); 
  include 'library/closedb.php';

  echo "<br>File $fileName uploaded<br>";
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/130159-php-upload-problem/#findComment-676547
Share on other sites

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.