473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Downloaded file automatically saved and overwrite previous

30
Hi

I have a php code that uploads a file to mysql db then I detrmine the id of the last inserted file and download the file .e user is asked if he wants to save the file and then asked for te file name.
I want te php to automatically save the file and overrides the previous downloaded file.
Any advise will be apprecited.

If 1stfile upload = name.txt and 2ndfile upload=surname.txt.Surname.txt must overwrite name.txt.

Expand|Select|Wrap|Line Numbers
  1. ?php
  2.     // Check if a file has been uploaded
  3.      if(isset($_FILES['uploaded_file'])) {
  4.      // Make sure the file was sent without errors
  5.      if($_FILES['uploaded_file']['error'] == 0) {
  6.          // Connect to the database
  7.          $dbLink = new mysqli("127.0.0.1","root","blabla",'blabla');//143.160.11.51
  8.          if(mysqli_connect_errno()) {
  9.          die("MySQL connection failed: ". mysqli_connect_error());
  10.          }
  11.          // Gather all required data
  12.  
  13.          $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
  14.          $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
  15.          $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
  16.          $size = intval($_FILES['uploaded_file']['size']);
  17.          // Create the SQL query
  18.          $query = "INSERT INTO file (`name`,`mime`,`size`,`data`,`created`)
  19.                    VALUES ('{$name}', '{$mime}', '{$size}', '{$data}', NOW())";
  20. // Execute the query
  21. $resulta = $dbLink->query($query);
  22. $getid="SELECT id FROM file ORDER BY created DESC LIMIT 1 ";
  23. $result2 = $dbLink->query($getid);
  24. $mysqli->error;
  25. $row1=$result2->fetch_assoc();
  26.  
  27.  
  28. // Close the mysql connection
  29. $dbLink->close();
  30. }}
  31. else
  32. {echo 'Error! A file was not sent!';}
  33.  /////////////////////////Download///////////////////////////////////////////
  34.  
  35. // Make sure an ID was passed 
  36.  
  37. // Get the ID 
  38. $id= $row1['id']; 
  39.  
  40. // Make sure the ID is in fact a valid ID 
  41. if($id <= 0) { 
  42. die('The ID is invalid!'); 
  43. else { 
  44.   // Connect to the database 
  45.  $dbLink = new mysqli("127.0.0.1","root","blabla",'blabla'); 
  46.  if(mysqli_connect_errno()) { 
  47.  die("MySQL connection failed: ". mysqli_connect_error()); 
  48.                             } 
  49. // Fetch the file information 
  50. $query = "SELECT `mime`, `name`, `size`, `data` 
  51. FROM `file` 
  52. WHERE `id` = {$id}"; 
  53. $resultb=$dbLink->query($query); 
  54.  
  55. if($resultb) {
  56. // Make sure the result is valid 
  57. if($resultb->num_rows == 1) {
  58. $rowb =mysqli_fetch_assoc($resultb); 
  59. // Print headers
  60. header("Content-Type: ".$rowb['mime']);
  61. header("Content-Length: ".$rowb['size']);
  62. header("Content-Disposition: attachment; filename=".$rowb['name']);
  63. // Print data
  64. echo $rowb['data'];
  65.  } 
  66. else { 
  67. echo 'Error! No image exists with that ID.'; 
  68. // Free the mysqli resources 
  69. @mysqli_free_result($resultb); 
  70. else { echo "Error! Query failed: <pre>{$dbLink->error}</pre>"; } 
  71. @mysqli_close($dbLink); 
  72. }
  73. echo 'end';
  74. echo $rowb['data'];
  75. ?>
  76.  
Aug 19 '10 #1

✓ answered by TheServant

How about instead of:
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO file (`name`,`mime`,`size`,`data`,`created`)
  2.                    VALUES ('{$name}', '{$mime}', '{$size}', '{$data}', NOW())";
You use:
Expand|Select|Wrap|Line Numbers
  1. $prev_id = "ID you want to replace";
  2. $query = "UPDATE file SET `name`='{$name}', `mime`='{$mime}', `size`='{$size}', `data`='{$data}', `created`=NOW() WHERE id='{$prev_id}' LIMIT 1";
With a little if clause, that only UPDATE if $prev_id is set, otherwise use INSERT as you have before.

4 2748
TheServant
1,168 Expert 1GB
How about instead of:
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO file (`name`,`mime`,`size`,`data`,`created`)
  2.                    VALUES ('{$name}', '{$mime}', '{$size}', '{$data}', NOW())";
You use:
Expand|Select|Wrap|Line Numbers
  1. $prev_id = "ID you want to replace";
  2. $query = "UPDATE file SET `name`='{$name}', `mime`='{$mime}', `size`='{$size}', `data`='{$data}', `created`=NOW() WHERE id='{$prev_id}' LIMIT 1";
With a little if clause, that only UPDATE if $prev_id is set, otherwise use INSERT as you have before.
Aug 20 '10 #2
Tinus
30
Hi
Thanks for the reply.
I dont want the file in the database to be overwritten.
I want the file that is stored in the specific directory on the server.

1.File upload to database.
2.Store file in directory on pc
3.Do things with the file in /weqw/wer/..

Nxt time a file is uploaded
1.strore in db
2.overwrite file thatwas previously downloaded to directory.
3. do some stuff with the file.

Sorry for not stating the issue more clearly.
Aug 20 '10 #3
code green
1,726 Expert 1GB
I dont want the file in the database to be overwritten.
I want the file that is stored in the specific directory on the server.
TheServant misunderstood because the code supplied is for database interaction.
There is no code for saving the file anywhere on the server.
Aug 20 '10 #4
Tinus
30
The download section does provide the user with question on if he would like to open or save the file.(which is downloaded from the DB)

Thanks for the help.

I have changed the code to upload the file to database and store the file in a directory(not download from DB) and it's doing what I wanted.
Aug 20 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: m. verkerk | last post by:
Hi everybody! Hope someone can help me out with this! I'm sending a file to a user with the following code: header( "Content-Type: application/binary"); header( "Content-disposition:...
3
by: Jay | last post by:
Hey there, Is there a way to have php automatically save a file that is being downloaded without having the annoying "Save As.." dialog box that appears? I would like to be able to create a txt...
1
by: Ken Sturgeon | last post by:
In VS2005 I have a loop that grabs all of the html from a number of web pages and saves them as files on a local drive. While all of the files get saved, several of the files are empty, although I...
3
by: Knick via AccessMonster.com | last post by:
I am novice to MS Access Development, I regularly back my access database every 2, 3 days ...today when I was going to back my database I saw the access file on the network drive was only 9 MB...
1
by: imburgiadom | last post by:
From a Microsoft Access form command button, I want to launch paperport and open a selected pdf file automatically. I have associated pdf files with paperport 11. The VBA code that I use...
1
by: =?Utf-8?B?QW5ndXM=?= | last post by:
New user. Where do E-mails go once they are no longer seen on E-mail incoming mail screen? Are they automatically saved in some folder? If so: what is the name of that folder? Thanks, Angus
1
by: campbellbrian2001 | last post by:
I have a product inventory database with a form to enter new products. I used the wizard to add a button to save the new item upon pressing it, however I noticed that the record is saved anyway....
1
by: ABC | last post by:
Hi, I found many methods to download attachment files, but I cannot found any to force the browser open excel file if the download is excel type. Is there any reference or tutorial on internet?
2
by: b00gieman | last post by:
Hi! Is it possible to trigger an action after a file is saved? Thanks!
0
by: makinha | last post by:
Hello All, I can use WebClient.DownloadFile Method to download target file from the website, like WebClient.DownloadFile("http://www.abc.com/a.pdf", "D:\a.pdf") but how can I verify the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.