Saving Filepath Of Uploaded Image To MySQL Database

后端 未结 1 805
逝去的感伤
逝去的感伤 2021-01-15 10:23

I have gone through countless different help menus and topics for this and still having problems. I simply want to insert the filepath of an uploaded image into a MySQL data

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 10:50

    Security issues and deprecated extension aside, all you need to do is insert the file name to the database. To do that, add a "filename" field to your database and then adjust your insert query accordingly:

    INSERT INTO products (name, description, price_low, price_high, filename)
                  VALUES (:name, :desc, :price_low, :price_high, :filename)
    

    Also, your $uploaddir variable is empty, the files probably aren't even being saved anywhere at the moment. To move your files properly, try something like this:

    $uploaddir = '/path/where/you/can/save/';
    $rawFilename = $_FILES['userfile']['name'];
    $extension = pathinfo($rawFilename, PATHINFO_EXTENSION);
    
    $uploadfile = $uploaddir . md5($rawFilename) . '.' . $extension;
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Upload failed";
    }
    

    This script assumes you trust the uploaded content and the md5 function is just is just a quick and easy way to "sanitize" (if I can call it that) the file's name.

    0 讨论(0)
提交回复
热议问题