Boldie
Boldie

Reputation: 25

PHP upload file, save file url to MySQL

Stuck with this, tried from ajax to w3schools version of uploading but no go.

My _events.php script contains the following to get the file information:

<input name="event_image" type="file" />

My _eventscreate.php script has no way of uploading the file to my _gallery/ directory, once uploaded i would like to url to the file to be saved to my database (see SQL query).

<?php   // Get Event ID
    $location=$_GET['location'];

    // Get values from form
    $event_name=$_POST['event_name'];
    $event_description=$_POST['event_description'];
    $event_date=$_POST['event_date'];
    $event_time=$_POST['event_time'];
    $event_cost=$_POST['event_cost'];
    $event_image=$_POST['event_image'];

    // Connection to MySQL Database.
    include ('_includes/_dbconnection.php');
    include ('_includes/_dbopen.php');

    // Update Event using Event ID.

    $sql="INSERT INTO b_events (ename, edescription, edate, etime, ecost, locationid, eimage)VALUES('$event_name', '$event_description', '$event_date', '$event_time', '$event_cost', '$location', '$event_image')";
    $result=mysql_query($sql);

    if($result){
    header('Location: _events.php');
    }

    else {
    header('Location: _home.php');
    }

?>

Please Help

Upvotes: 0

Views: 3088

Answers (2)

Muhammad Alvin
Muhammad Alvin

Reputation: 1210

You can't use $event_image = $_POST['event_image']; to get the uploaded file.

To access the uploaded file, you have to use $_FILES[$name], where $name is the name of:

<input type="file" name="something" />

Also make sure that your form has method="post" and enctype="multipart/form-data".

$_FILES[$name] itself will be an array (associative array), with keys:

  • name
  • type
  • size
  • tmp_name
  • error

After the file has been successfully uploaded (test with $_FILES[$name]['error'] == UPLOAD_ERR_OK), you need to move it to your preferred location.

After the file has been moved (by using move_uploaded_file()), you can get its name, and assign the value to your $event_image. Then save it to the database.

For further information, read this.

Upvotes: 0

Bhau
Bhau

Reputation: 26

By URL, I'm going to assume you mean the path to the local copy of the file rather than the remote source location. If I'm mistaken about that, the answer is probably "Don't use a file upload".

When your php script gets executed (unless you've done anything particularly strange), it means that the file upload has completed and the files information (name, size etc) is being stored in $_FILE. Before you're able to store or use its path (or indeed move it to _gallery) you need to set that path. Once uploaded the file is stored in the path in $_FILES[fileID]["tmp_name"]. However you can't directly operate on this path, for example with move or read operations (at least I couldn't in Apache2).

First, you need to write that file out to a location of your choosing, via PHP's inbuit methods. For example:

move_uploaded_file($_FILES[fileID]["tmp_name"], "_gallery/".$_FILES[fileID]["name"]);

After which point, the file is yours to do with as you wish, and it's address ("_gallery/".$_FILES[fileID]["name"] in the above example) is just another PHP string.

If by URL you mean the outward facing address of that file (what someone would put into a browser to get to the file), then you'll need to construct it yourself. In the simplest case that the file is within the webroot, that means concatenating the relative path to the file you've just written. If you don't want to use a hardcoded base url, the values in $_SERVER might be useful to you.

Upvotes: 1

Related Questions