0

I have a PHP running that uploads a file to my server, I then do some calculations with this file with another script and upload some data (the results of the calculations) to mySQL. After that, I read this data (the results of the calculations) from mySQL in this same PHP that I used to upload the file to the server. The problem is that I don't know how long it will take to process this file in my server, it may take 5 seconds or 2 minutes, so now I'm using a sleep(40), because most files process in less time (but not all...) to make sure the values I'm looking for in mySQL will be there, but that's not the way it should be done. Is there any way to make the program wait until the values I'm looking for (the results of the calculations that are made with the file I just uploaded) are stored in mySQL? Something that constantly checks if the values are there and when they do continues running the program? I think it may not be possible with PHP and I may need a javascript. This is my code:

$uploaddir = '/opt/lampp/htdocs/stl';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$link = mysql_connect("whatever", "whatever...", "whatever...whatever");
$nombre=basename($_FILES['userfile']['name']);
$tamany=basename($_FILES['userfile']['size']);
$sql="INSERT INTO `wordpress`.`uploads` (`autoinc`, `nombre`, `tamany`, `fecha` , `cantidad`) VALUES (NULL, '$nombre', '$tamany', CURRENT_TIMESTAMP , '$cantidad')";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    mysql_select_db('wordpress');
    mysql_query($sql);
    rename ( $uploadfile,$uploaddir."/".mysql_insert_id().".stl");
    $uploadfile_we=basename($uploadfile,".stl");
    $numero=mysql_insert_id();
    echo '<pre>';
    sleep(40);
    $sqls= "SELECT `cond` FROM `cm` WHERE `id`='$numero'";
    $result = mysql_query($sqls);
    $row = mysql_fetch_assoc($result);
    $condicion = $row['cond'];
}

As you can see, a file is uploded and then I look in the database. How can I know if the database has already been updated or not?

Mr.Polymath
  • 67
  • 1
  • 11
  • 1
    it's very much achievable in PHP but you have to post your code what you have done so far, this is not a code writing service and no will will write a code for you. – Shehary Jul 18 '15 at 19:19
  • I'm sorry @shehary if I bothered you. I had no intention of you writting code for me, just guiding on how to do that. I'll edit my question now and show what I have so far so that you or others can help me. I'm new here as you can see in my profile. Bad start with two negative votes... :( – Mr.Polymath Jul 18 '15 at 19:28
  • @u_mulder I've got a sleep(40) because most files process in less than that time, but waiting the same for big files and small files is bad, and sometimes a file takes more than 40 seconds so it's even worst, but then the waiting time is too much. – Mr.Polymath Jul 18 '15 at 19:30
  • please post some source code – Nick Jul 18 '15 at 19:30
  • @shehary just edited it with the code – Mr.Polymath Jul 18 '15 at 19:46
  • @Nick I just posted some code, thank you for your advise – Mr.Polymath Jul 18 '15 at 19:47
  • I'm curious, how big is the data? It seems strange to me it could take 2 minutes to update using mySQL. Good luck – Script Kitty Jul 18 '15 at 20:06
  • @ScriptKitty it doesn't take 2 minutes to update mySQL! What I have to wait for is for my other script to process the file. First I upload my file to my server, then I do some calculations with it with another script that can take even more than 2 minutes, and when I get a result, I update mySQL. But how do I know from this same PHP that I use to upload the file that mySQL has been updated with the data I'm looking for? – Mr.Polymath Jul 18 '15 at 20:11
  • @Mr.Polymath, I'm sorry for my rudeness, it's just we can't see your computer screen so code is part of question and for us to see what you are asking is answerable or not. – Shehary Jul 18 '15 at 20:12
  • @Mr.Polymath, if I understand correctly, you are trying to upload a file (forget the size of file of a moment) and then you are running another script which i assume is also PHP and you wana see that the file is uploaded and your database has been updated or not, correct me if I'm wrong – Shehary Jul 18 '15 at 20:14
  • @shehary , I upload a file, I run another script that makes calculations with the file that has ALREADY been uploaded (That's not an issue) and when the calculations have finished, I upload the results to the database. What I need is to know, in my first PHP script, the one I use to upload the file, when are those values uploaded (they didn't exist before). Something that checks constantly if they exist. Thank you! – Mr.Polymath Jul 18 '15 at 20:19
  • then in short `if/else statement` is your best buddy and can you break your code in your question into steps like this is step where file is uploaded, 2nd step inserting data into DB, 3rd step fetching data from DB, 4th step checking the data. e.g "//Here fetching data and compareing $sqls= "SELECT `cond` FROM `cm` WHERE `id`='$numero'";" – Shehary Jul 18 '15 at 20:24

4 Answers4

1

This may get you started in the right direction: http://php.net/manual/en/session.upload-progress.php

Edit in response to comment: The only way I can this is to use JavaScript (Get file size before uploading) to get the file size and then have PHP compare that size to $_FILES['filename']['size'].

Community
  • 1
  • 1
  • Thank your for your answer but I think that this doesn't help me. The problem isn't that I don't know when the file is uploded. After I upload the file, I process it. This process may take 5 seconds or 1 minute, and after that, I put some data in mySQL. What I need is to know when this data is added to mySQL so that I can keep running my PHP. Thank you! – Mr.Polymath Jul 18 '15 at 19:48
  • Down voting an answer because the question was unclear is not cool! – Kevin Finkenbinder Jul 18 '15 at 20:13
  • I didn't down vote anything! I politely told him that this wasn't what I was looking for and corrected my mistake by editing a bit my question, and also thanked him for answering! I would NEVER down vote someone that is trying to help me! That's just not the way it works! I understand your anger. – Mr.Polymath Jul 18 '15 at 20:24
  • @Mr.Polymath don't worry he's not talking to you, you need 125 reps to downvote. I thought this was a good answer. (That wasn't a reverse-vote) – Script Kitty Jul 18 '15 at 20:26
  • @Mr.Polymath, Script Kitty is correct, I am not thinking you down-voted. – Kevin Finkenbinder Jul 20 '15 at 12:33
1

Without getting into code you could use the database to synchronize your processes.

  1. Place a row in the database upon upload. This will (can be used to) generate an ID value for the new file.

  2. Process the file, updating a flag on the file's record (identified by ID) which indicates that processing is complete.

  3. In the original code, have a loop that will wait some small period of time, perhaps a second, before checking the database state. Once the file has been processed, or too much time has passed, take appropriate action.

There are other ways to pass information back and forth between processes but it seems like you already are doing things in the database that are related to the file being uploaded.

A Smith
  • 621
  • 1
  • 4
  • 10
  • I like this! It's not exactly what I was looking for but would definetly work as good as what I was asking for. I'll try it and tell you something! Thank you! – Mr.Polymath Jul 18 '15 at 20:43
  • That's just waste of queries. – klenium Jul 18 '15 at 20:55
  • 1
    To get into a very efficient solution is way beyond the question at this point. However, a single record query by ID won't be causing much database effort -- at whatever reasonable interval chosen. – A Smith Jul 18 '15 at 21:03
  • I got it to work perfectly. Thank you very much! – Mr.Polymath Jul 23 '15 at 06:50
  • That's great! It's always nice to avoid "polling" the database but some low effort queries against a table with only a few records will scale for a quite a while. – A Smith Jul 23 '15 at 09:09
  • If you got the Solution then Post Code here... – Anurag Rabadia Feb 16 '18 at 14:36
0

Mysql will return true after successful insert statement. So check the value is true and execute rest of your code.

Sk_
  • 1,051
  • 8
  • 18
  • Thank you for your answer, but i don't know how to use that. After I upload the file to my server, I process it with another script and I upload some data to mySQL with another script. But what I need is to know, from this PHP, when are this values being stored in the database. Something that checks if these values exist continuously until they do. – Mr.Polymath Jul 18 '15 at 19:57
  • @Mr.Polymath I feel like this could be a good answer of what you're looking for. Like, if you are using prepared statements initialize `$uploadedYet = false` and let `$uploadedYet = $query->execute()`. Then on the part where you have to check if the calculation is down yet, $uploadedYet will turn to true when you are ready to go. Hope you find a solution. – Script Kitty Jul 18 '15 at 20:25
  • @ScriptKitty I'm sorry for not knowing that, I'm a student of aerospace engineering really interested in programming and although I try my best I'm still a complete noob. What does $query->execute() do and how do I use it? I tried to find information about it on the internet but I can't manage to find anything that explains it to me clear. Thank you very much for your help! – Mr.Polymath Jul 18 '15 at 21:56
  • @Mr.Polymath Don't apologize, I don't know more than a few scratches of PHP. I'm not sure if it'll even work, don't worry about it and look at the link Kevin posted. Sorry for being pedantic, I will explain what I mean. You know how to update a db through SQL from your question, right? PREPARED statements is just another way to do it, that is arguably more secure. `$dbh = new PDO ("mysql:dbname=YOURDBNAME; host=127.0.0.1", "root", "password"); $sql = $dbh->prepare("SELECTblahblah"); $sql->execute();` Don't worry about it, it's just an alternate way of updating the table. Enjoy learning php – Script Kitty Jul 18 '15 at 22:13
0

You don't need this. The cm table isn't updated. The insert query don't affect the values stored in this table, you can select data from it at any time, the data will be the same.

After reading your comments, I can't understand why would you run two different PHP script at the same time? I gues you call them via ajax, right after the other? But why would you do it? Just include/require the process script after move_uploaded_file returns ture, update the tables, and then use the data. You save one query, you don't need to select what you've just upldated.

klenium
  • 2,468
  • 2
  • 24
  • 47