gsamaras
gsamaras

Reputation: 73366

move_uploaded_file: no such file when another directory is used

I am using this answer, where they use:

move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);

which works like a charm, if I create an 'uploads' directory inside the 'php' directory that executes this code.

If I try to do this:

move_uploaded_file($_FILES['file']['tmp_name'], '../img/' . $_FILES['file']['name']);

I am getting this warning:

enter image description here

What am I missing here? I tried with absolute path too!

Directory tree:

scouter
  -img
  -php
    -uplodas
    -upload.php

'img' directory seems to have the needed permission options.


EDIT_0:

If I place the file in uploads dir and then use rename() it will place the file in img dir...Can't get what is happening!


EDIT_1:

HTML:

<input id="input_pic" type='file' name='userFile'/>
<div>
  <img class="profile_pic" src="img/newname.jpg" alt="player_img" />
</div>

JS:

var file_data = $('#input_pic').prop('files')[0];   
var form_data = new FormData();                  
form_data.append('file', file_data);
alert(form_data);                             
$.ajax({
    url: 'php/upload.php',
    dataType: 'text',
    cache: false,
    contentType: false,
    processData: false,
    data: form_data,                         
    type: 'post',
    success: function(php_script_response){
      alert(php_script_response);
    }
});

EDIT_2:

The server response, from the answer of swidmann:

enter image description here

Upvotes: 0

Views: 505

Answers (1)

swidmann
swidmann

Reputation: 2792

You are using an other name in your HTML, I somehow doubt, that this was working before, your HTML:

<input id="input_pic" type='file' name='userFile'/>

Due to the php it should be:

<input id="input_pic" type='file' name='file'/>

or your php should use:

$_FILES['userFile']['tmp_name']

I think your problem is maybe the upload size, try this to debug:

    var_dump( $_FILES );
    $upload = move_uploaded_file( $_FILES['file']['tmp_name'], dirname( dirname( __FILE__ ) ).'/img/'. $_FILES['file']['name'] );
    var_dump( $upload );
    var_dump( ini_get("upload_max_filesize") );// what is allowed and how big is your picture?
    var_dump( $_FILES['file']['size']  );// if this is 0, maybe your allowed upload size is to low

By the way I tried this and it worked:

dirname( dirname( __FILE__ ) ).'/img/'

To change the allowed upload size, you have to change the following options: Set upload_max_filesize and post_max_size in your php.ini.

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

If this is all OK, please try the absolute path:

move_uploaded_file( $_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"]."/test/scouter/img/".$_FILES['file']['name'] );

Upvotes: 2

Related Questions