1

I got error while moving from tmp to location... Here's my code

if(isset($_POST['upload']))
{
    $name = $_POST['name'];
    $album_id = $_POST['album']; 
    $file = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];
    $file_tmp = $_FILES['file']['tmp'];
    $random_name = rand();

    if(empty($name) || empty($file))
    {
        echo "Please fill all the fields";
    }
    else
    {
        move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');//Error in this line
        mysqli_query($con,"INSERT INTO `p_photos` (`name`,`album_id`,`url`) VALUES ('$name','$album_id','$random_name.jpg')");
        echo 'Photo Uploaded successfully!<br /><br />';        

    }

}

Where did i go wrong....yes there is a folder 'uploads'

shaiToro
  • 137
  • 1
  • 3
  • 10
  • The **undefined index** error would come from trying to access an index [`tmp`], such as on this line: `$file_tmp = $_FILES['file']['tmp'];`. Check to make sure `$file_tmp` is defined properly. – Tim Lewis Oct 23 '15 at 20:01
  • yup...ur right. where did i go wrong? – shaiToro Oct 23 '15 at 20:03
  • http://php.net/manual/en/features.file-upload.php – Pedro Lobito Oct 23 '15 at 20:03
  • Run `var_dump($_FILES['file']);` and see if `['tmp']` is an available index. Otherwise, use the correct one (which I think is `tmp_name`, but I haven't worked with raw file uploads in a long time) – Tim Lewis Oct 23 '15 at 20:06

2 Answers2

2

Where did i go wrong....yes there is a folder 'uploads'

The key tmp doesn't exist, the correct is tmp_name, also, make sure the folder uploads is writable.

if(isset($_POST['upload']))
{
    $name = $_POST['name'];
    $album_id = $_POST['album']; 
    $file = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];
    $file_tmp = $_FILES['file']['tmp_name']; //The error is here
    $random_name = rand();

    if(empty($name) || empty($file))
    {
        echo "Please fill all the fields";
    }
    else
    {
        move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');//Error in this line
        mysqli_query($con,"INSERT INTO `p_photos` (`name`,`album_id`,`url`) VALUES ('$name','$album_id','$random_name.jpg')");
        echo 'Photo Uploaded successfully!<br /><br />';        

    }

}

Learn more about php file uploads

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

The property you're looking for is tmp_name, rather than tmp

David Stromberg
  • 244
  • 1
  • 6