ddtpmyra Posted September 29, 2008 Share Posted September 29, 2008 Hi I am almost done with my new application build in PHP and mysql. But the last point of my testing the file attach I download thru PHP to MYSQL is corrupted. Maybe any one can help here who had same proble before or knowledge how to handle this error. 1. form to upload <form action="add_file2.php" method="POST" enctype="multipart/form-data"> <p><b>Author(s):</b> <br> <input type="text" name="author" size="80" > <br> <br> <b>Requestor Name(s):</b> <br> <input type="text" name="requestor" size="80" > <br> <br> <b>Deadline Feedback:</b> <script>DateInput('deadlinefeedback', true, 'YYYY-MM-DD')</script> <!--<input type="button" onClick="alert(this.form.deadlinefeedback.value)" value="Show date value passed"> --> <br> <br> <b>Select CMR Category<br> <br> <select name="category"> <option value="Process Change">Process Change</option> <option value="Product Update">Product Update</option> </select> </b></p> <p><b>Brief Description:</b> <br> <input type="text" name="description" size="80" height="550" > <br> <b>File to upload:</b> <br> <input type="file" name="uploaded_file"> <br> <br> <input type="submit" value="Upload file"> </p> </form> 2. adding the file <?php # Check if a file has been uploaded if(isset($_FILES['uploaded_file'])) { # Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { # Connect to the database $dbLink = mysql_connect("xxx", "xxx","xxx", "xxx") or die("Error! Failed to connect to the MySQL server!"); mysql_select_db("cmr", $dbLink) or die("Error! Failed to select a database!"); # Gather all required data $name = mysql_real_escape_string($_FILES['uploaded_file']['name'], $dbLink); $mime = mysql_real_escape_string($_FILES['uploaded_file']['type'], $dbLink); $size = $_FILES['uploaded_file']['size']; $author = $_POST['author']; $requestor = $_POST['requestor']; $description = $_POST['description']; $deadlinefeedback = $_POST['deadlinefeedback']; $DateInput = $_POST['deadlinefeedback']; $category =$_POST['category']; $data = mysql_real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']), $dbLink); # Create the SQL query $query = " INSERT INTO fileStorage ( FileName, FileMime, FileSize, FileData, Created, Description,Author, Requestor, DeadLineFeedback, category ) VALUES ( '{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$description}', UPPER('{$author}'), UPPER('{$requestor}'), '{$DateInput}','{$category}' )"; # Execute the query $result = mysql_query($query, $dbLink); # Check if it was successfull if($result) { echo "Success! Your file was successfully added!"; } else { echo "Error! Failed to insert the file"; echo "<pre>". mysql_error($dbLink) ."</pre>"; } } else { echo "Error! An error accured while the file was being uploaded. Error code: ". $_FILES['uploaded_file']['error']; } # Close the mysql connection mysql_close($dbLink); } else { echo "Error! A file was not sent!"; } # Echo a link back to the mail page #echo '<p>Click <a href="cmr.php">here</a> to go back</p>'; #echo '<p>Click <a href="list_files2.php">here</a> View the files</p>'; ?> getting or uploading the file (here i have problems, everytime i open the file like in ms work the file is corrupted by the way I tried using Medium Blob on mysql) [code] <?php # Make sure a valid ID was passed if(isset($_GET['id'])) { # Get the ID $id = $_GET['id']; # Connect to the database $dbLink = mysql_connect("xxx3", "xx", "xx","xxx") or die("Error! Failed to connect to the MySQL server!"); mysql_select_db("cmr", $dbLink) or die("Error! Failed to select a database!"); # Fetch the file information $query = " SELECT FileMime, FileName, FileSize, FileData FROM fileStorage WHERE FileID = {$id}"; $result = @mysql_query($query, $dbLink) or die("Error! Query failed: <pre>". mysql_error($dbLink) ."</pre>"); # Make sure the result is valid if(mysql_num_rows($result) == 1) { # Get the row $row = mysql_fetch_assoc($result); # Print headers header("Content-Type: ". $row['FileMime']); header("Content-Length: ". $row['FileSize']); header("Content-Disposition: attachment; filename=". $row['FileName']); # Print data echo $row['FileData']; } else { echo "Error! MySQL Query returned invalid results!"; } # Close the mysql connection @mysql_close($dbLink); } else { echo "Error! An invalid ID was passed!"; } ?> [/code] Link to comment https://forums.phpfreaks.com/topic/126318-corrupted-file/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.