Jump to content

Recommended Posts

Hi..

 

i want user to upload a file which is then stored in my database i have written this code but it is not working.

 

<?php

if (isset($_GET['try'])) {   
   

      $allowed_filetypes = array('.JPG','.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
      $upload_path = ''; // The place the files will be uploaded to (currently a 'files' directory).

   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.



  
   if(!in_array($ext,$allowed_filetypes))
      die('The file you attempted to upload is not allowed.');

   
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
      die('The file you attempted to upload is too large.');

   // Check if we can upload to the specified path, if not DIE and inform the user.


   // Upload the file to your specified path.

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)){
         echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; 

        $instr = fopen("skill.JPG","rb");
        $image = addslashes(fread($instr,filesize("skill.JPG")));
        $insert = mysql_query ("insert into pix (imgdata) values('$image')");



   }
      else
         echo 'There was an error during the file upload.  Please try again.'; 
}
?>

<HTML>
<form action="upload.php?try=true" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>

</HTML>
</form>

 

 

Please Help!

Link to comment
https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/
Share on other sites

i want user to upload a file which is then stored in my database i have written this code but it is not working.

What is it that's not working? what method are you using for your form are you using the post method and are you using enctype="multipart/form-data"?

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

Your are closing your html tag before you close your form tag.

 

Your code:

<HTML>
<form action="upload.php?try=true" method="post" enctype="multipart/form-data">
   <p>
      <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br />
      <button>Upload File</button>
   <p>

</HTML>
</form>

The only one here who can actually troubleshoot what your code is doing on your server is you. So, what have you done to pin down what exactly it is doing, especially since you did not state what it does when you try it. Just telling us that code does not work is pointless, we already know that because you would not be witting a question about it on a programming help forum if it did work. What do you see in front of you? A blank page? The form just redisplays? A php error message? An error message from your logic?

 

Some things to check -

 

Have you checked if uploads are enabled on your server?

 

Have you used a print_r() statement to display what exactly is in the $_FILES array so you know your code is receiving the data you expect?

 

Is full php error reporting on (error_reporting set to E_ALL and display_errors set to ON) in your php.ini so that php would help you with errors it finds?

The html is working correctly even the code is working partially correct.

As it takes file and stores it in specified folder. But the file is not stored in database. i think there is some problem in sql command or the way i have written it.  I just want to take file from user and store it in database. If someone gets any such code on net please tell me.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.