Trying to upload file to MySQL, file not found

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mlevit
    New Member
    • Jun 2007
    • 30

    Trying to upload file to MySQL, file not found

    I'm trying to upload a file. I use to get the fread(): supplied argument is not a valid stream resource error, but then I placed the code in an if statement with file_exists.

    The problem is, the if statement never gets executed.

    Code:
    if ($_POST)
    	{
    		include ('scripts/db_connect.php');
    		
    		$reviewId = $_POST['review_id'];
    		$form_data = $_POST['form_data'];
    		
    		if (file_exists($form_data))
    		{
    			$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
    			
    			$query = "INSERT INTO uploads (report_id, report_data, report_filename, report_filesize, report_filetype)"
    						. "VALUES (" . $reviewId . ",'" . $data . "','" . $form_data_name . "','" . $form_data_size . "','" . $form_data_type. "')";
    					 
    			if (mysql_query($query))
    			{
    				// Upload successfull
    				?>
    				<div id="success_box">
    					<div id="success_box_text">The report has been uploaded successfully.</div>
    				</div>
    				<?php
    			}
    		}
    		else
    		{
    			echo "File not found";
    		}
    		
    		include ('scripts/db_close.php');
    	}
    Anyone know what's going on here?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    uploaded files are to be found in $_FILES not $_POST. that's why your if statement doesn't get executed.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      The file doesn't exist - simple. :P

      What does $form_data hold?

      Comment

      • mlevit
        New Member
        • Jun 2007
        • 30

        #4
        Ok I've updated my code, still nothing.

        Code:
        <form action="members.php" method="post" enctype="multipart/form-data">
        										Report Upload:<input type="file" name="form_data" id="form_data" size="30" />
        										<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
        										<input type="hidden" name="review_id" value="<?php echo $reviewID; ?>" />
        										<input class="submit" value="Upload" id="submit1" type="submit" style="text-align: center; float: none;"/>
        									</form>
        Code:
        include ('scripts/db_connect.php');
        		$reviewId = $_POST['review_id'];
        		
        		if(isset($_POST[’upload’]) && $_FILES['form_data'][’size’] > 0)
        		{
        			$fileName = $_FILES['form_data'][’name’];
        			$tmpName = $_FILES['form_data'][’tmp_name’];
        			$fileSize = $_FILES['form_data'][’size’];
        			$fileType = $_FILES['form_data'][’type’];
        			$fp = fopen($tmpName, ‘r’);
        			$content = addslashes($content);
        			fclose($fp);
        			
        			echo $fileName . "1\n";
        			echo $tmpName . "2\n";
        			echo $fileSize . "3\n";
        			echo $fileType . "4\n";
        			
        			if(!get_magic_quotes_gpc())
        			{
        				$fileName = addslashes($fileName);
        			}
        			
        			$query = "INSERT INTO report (report_id, report_data, report_filename, report_filesize, report_filetype)"
        								. "VALUES (" . $reviewId . ",'" . $content’ . "','" . $fileName’ . "','" . $fileSize’ . "','" . $fileType’. "')";
        
        			$result = mysql_query($query);
        			
        			if (!$result) 
        			{
        				echo mysql_error();
        			}
        		}
        		
        		include ('scripts/db_close.php');
        It doesn't enter the IF statement because $_FILES['form_data'][’size’] is never more than zero. Why doesn't it see the file?

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          You have no form element with the name 'upload', so the if conditional fails.

          Comment

          • mlevit
            New Member
            • Jun 2007
            • 30

            #6
            Fair enough. I've removed that part of the if statement but it still doesn't pass the

            $_FILES['form_data'][’size’] > 0

            part. Can anyone recommend me an upload script they actually know works?

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

              Comment

              Working...