#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Name uploaded file appropriately


    Using MySQL and PHP to manipulate.

    I have a page where a user can submit a file and it stores the file location in the database, and the file on the server. I'm trying to properly name the file as it is uploaded, as the auto incremented sql id field of the brand newly created mysql record(row) that is created when i submit the file and the details of record at the same time. When the form is submitted however, the record is just then created, and the auto incremented ID is then created after the image upload - how can I name this file appropriately to match the record?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I Want to name the file the same as mdl_key which is the auto incremented integer in the mysql Model table
    RELEVANT CODE::


    PHP Code:
    // Check if form has been submitted.
    if (isset($_POST['submitted'])) {

        
    $errors = array(); // initilize error array.

        //HANDLE FILE UPLOADS

            // PDF FILES
                //This is the directory where images will be saved
                    
    $target "./pdf/";
                    
    $target $target basename$_FILES['pdf']['name']);


        
    // handle drop boxes
        
    $man_key $_POST['manufacturer'];
        
    $cls_key $_POST['class'];
        
    $sub_cls_key $_POST['subclass'];
        
    $pdf=($_FILES['pdf']['name']);

        if (empty(
    $_POST['video'])) {
            
    $video NULL;
        } else {
            
    $video trim($_POST['video']);
        }

        if (empty(
    $_POST['description'])) {
            
    $description NULL;
        } else {
            
    $description trim($_POST['description']);
        }


            
    // Check for a model key
        
    if (empty($_POST['model'])) {
            
    $errors[] = 'You forgot to enter a model.';
        } else {
            
    $model trim($_POST['model']);
        }

        if ((empty(
    $_POST['rent'])) && (empty($_POST['sell']))) {
            
    $errors[] = 'You must select either Rentable or Sellable checkbox.';
        }


        if (
    $_POST['rent']) {
            
    $rent 1;

                if (
    $_POST['rentprice']) {
                    
    $rentprice $_POST['rentprice'];
                } else {
                    
    $errors[] = 'If you select rentable, you must enter a rental price.';
                    
    $rentprice 0;

                }

        } else {
            
    $rent 0;
            
    $rentprice 0;
        }

        if (
    $_POST['sell']) {
            
    $sell 1;

                if (
    $_POST['sellprice']) {
                    
    $sellprice $_POST['sellprice'];
                } else {
                    
    $errors[] = 'If you select sellable, you must enter a sale price.';
                    
    $sellprice 0;
                }

        } else {
            
    $sell 0;
            
    $sellprice 0;
        }


        if (empty(
    $errors)) { // Everything is ok

            /// Add inventory item
            
    require_once ('../mysql_connect.php'); // Connect to the DB.

            // Make Query
            
    $queryInsert "INSERT INTO model (man_key, cls_key, sub_cls_key, model, rent, sell, rentprice, sellprice, description, video, pdf)
            VALUES ('
    $man_key', '$cls_key', '$sub_cls_key', '$model', '$rent', '$sell', '$rentprice', '$sellprice', '$description', '$video', '$pdf')";
            
    $resultInsert = @mysql_query ($queryInsert); // Run Query


            
    if(move_uploaded_file($_FILES['pdf']['tmp_name'], $target))
            {

                
    //Tells you if its all ok
            
    echo "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
            } else {

            
    //Gives and error if its not
            
    echo "Sorry, there was a problem uploading your file.";
            } 
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I guess I could put the uploaded pdf file name directly in the database record, so it forms a relationship with the rest of the new record - instead of using the mdl_key as the image name, to make the relationship.

    But I'm rusty with array's. How would I do this?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I don't see this "mdl_key" anywhere in your code. But the function mysql_insert_id() will return the ID of the last row you inserted.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Originally Posted by Jacques1
    Hi,

    I don't see this "mdl_key" anywhere in your code. But the function mysql_insert_id() will return the ID of the last row you inserted.
    The mdl_key is auto inc. and wasn't nessecary to include in query

    Thanks, this is useful. Since the pdf file that is uploaded with the record is inserted into the table at the time of the record - how should I go about utilizing this? Should I remove the $pdf fields from the original queryInsert = "", then use the mysql_insert_id() and rename the file to mysql_insert_id value directly below that. Finally, run a separate queryInsertFILE = "" with the $pdf variable placed in the record from here, so as to include the proper file name of the newly created rows auto inc ID?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    When the pdf file has the row ID as the name, then you don't need to store the file name in the table.

    Another option would be to give the file a random name (which is more secure) and store it in the table. But you have to make sure that the name is actually random (e. g. use openssql_random_pseudo_bytes) so that there are no collisions.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Great, then I can just make the retrieval of the records pdf file something like echo "$folder . '/' . $mdl_key . '.pdf'";

    When I'm doing this method, I will remove the $pdf field from the queryInsert. How do I handle renaming the file after the record is inserted though? move_uploaded_file?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by BitZoid
    When I'm doing this method, I will remove the $pdf field from the queryInsert. How do I handle renaming the file after the record is inserted though? move_uploaded_file?
    Yes. I mean, the query and the file don't really have anything to do with each other. It's just that you need to wait with the renaming until the row has been inserted to get the ID.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    Originally Posted by Jacques1
    Yes. I mean, the query and the file don't really have anything to do with each other. It's just that you need to wait with the renaming until the row has been inserted to get the ID.
    Thank you for your help. Got it.

IMN logo majestic logo threadwatch logo seochat tools logo