Max Posted December 27, 2016 Share Posted December 27, 2016 Hi Larry and everyone I have an academic type question so won't bore you with too much code. I am using the filenames generated by my camera (e.g. P9100399.JPG), uploading them, and then adding comments for a website later when I have time. I know you don't like doing this but hey! When uploading an image file, it is nice to check that it hasn't been uploaded to the same file previously. - Select file - Check if it exists on PC / size etc. etc. - Check if there is a matching filename using "if (file_exists(strtolower($_FILES['upload']['name'])))" - If exists, then "File exists on system. Do you want to overwrite?<a href upload_file2.php...etc>YES</a>" - Use SESSIONs to send $_FILES['upload']['temp_file] to upload_file2.php - Overwrite file with new image using SESSIONs to grab the temp filename. - Save filename to MySQL database with other data such as where taken, comments (to be updated later whenever), using the filename minus the file extension as the index, so it is therefore unique. The problem is that whether using $_POST or <a href> you seem to lose the temp file off of the server - I guess PHP erases it when you call recursively or to another program. What it means (when I'm programming anyway) is that if you want to overwrite the file, you need to re-start the process again (ignoring the 'file_exists' part) thus requiring the user to re-select the image. It works fine but I just feel that it is a bit clunky. Maybe Javascript would be a solution? Link to comment Share on other sites More sharing options...
Larry Posted January 7, 2017 Author Share Posted January 7, 2017 Ah! Interesting problem. Yes, a temporary file is, by definition, temporary, so your second step means you lose access to it. JavaScript can't really help as it's client-side. But what you can do is make the temporary file not temporary by moving it to an intermediary location/filename. Then you would make the official move if appropriate. The only thing to watch out for is if the final step is never taken you could have phantom unnecessary files, so I'd recommend putting these into a new directory during the intermediary step so you can always go back in and clear out that directory later (i.e., perform garbage collection). Link to comment Share on other sites More sharing options...
Max Posted January 18, 2017 Share Posted January 18, 2017 Sounds like the perfect solution - you could then do housekeeping once a month to delete any files hanging around kicking their heels. I'll have a look at the code and re-write it. Regards Max Link to comment Share on other sites More sharing options...
gudchyld Posted August 3, 2017 Share Posted August 3, 2017 Been trying my best at the review and pursue section for chapter 15, login_ajax.php where it said *Modify login_ajax.php so that it usesa database to confirm successful login.* this is what i've tried so far, whatever i do, the response i get seems to always be "INCORRECT" and then the ajax script never logs me in my code so far: <?php if (isset($_GET['email'], $_GET['password'])){ $email = $_GET['email']; $password = $_GET['password']; // Need a valid email address: if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // must match specific values: // This values will be gotten from a database require('../mysqli_connect.php'); // retrieve from database $q = "SELECT email, pass FROM users WHERE email = '$email'LIMIT 1"; // run the query $r = @mysqli_query($dbc, $q); //$check_email = ""; //$check_pass = ""; /*while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $check_email = $row['email']; $check_pass = $row['pass']; }*/ // if the email and password match those in database //if (($email == $check_email && $password == $check_pass)) { if (mysqli_num_rows($r) > 0) { echo 'CORRECT'; } else { echo 'INCORRECT'; } mysqli_close($dbc); /* if(($_GET['email'] == 'email@example.com') && ($_GET['password'] == 'testpass')){ //Set a cookie, if you want, or start a session. // indicate success: echo 'CORRECT'; }else{// mismatch echo'INCORRECT'; }*/ }else{ // invalid email echo 'INVALID_EMAIL'; } }else{ // missing one of the two variables echo 'INCOMPLETE'; } it contains my different tries, please i need urgent review Link to comment Share on other sites More sharing options...
Larry Posted October 14, 2017 Author Share Posted October 14, 2017 So sorry for the delayed reply. This got lost on my end. I hope you've figured it out by now but if not, let me know, sharing the current version of the code. With what you posted, it looks like your query has a syntax error (missing a space before "LIMIT"). Apologies again! Link to comment Share on other sites More sharing options...
Recommended Posts