Dan
Dan

Reputation: 35

Moving an uploaded file using PHP

please could someone check this for me? I have followed the info that I found on here and still I cant get it to work. My host says that this should now be possible as the permissions were previously blocked, and are not exactly the most helpful. Thanks, I can always rely on StackOverflow to help out when needed. :)

<?PHP
IF(isset($_POST['submit'])){
    $caption = $_POST['caption'];
    $file = $_FILES['file']['name'];
    $target="images/slider";
    if(is_uploaded_file($_FILES['file']['tmp_name'])){

    move_uploaded_file($_FILES['file']['tmp_name'], 'images/slider/'.$_FILES['file']['name']) or die ('cannot upload');

    require_once('../includes/dbupcombo.php');

        $query = "INSERT INTO PremierSlider (caption, image) values ('$caption','$file')";
        mysql_query($query) or die(mysql_error());


}
header("location: http://www.premierdancecentre.com/admin/index.php#mod_image");
}
?>

The script runs onclick and returns 'cannot upload'. Any help as to why would be greatly appreciated as i'm still trying to get good at PHP. Cheers guys

Upvotes: 1

Views: 15860

Answers (2)

Aamir Mahmood
Aamir Mahmood

Reputation: 2724

First of all please check that you are using a proper form tag

<form action="upload_file.php" method="post" enctype="multipart/form-data">

check that you have enctype="multipart/form-data" on form.

in upload_file.php

check that you are really getting the file by doing

var_dump($_FILES)

if yes, then you can use your move_uploaded_file

Upvotes: 2

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

<?php

  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }


?> 

Upvotes: 3

Related Questions