0

I have a problem with my upload file function. I'm following this website to create the upload form to upload a text file and i just modify it a little. Here's the code:

upload_form.php :

//the jquery script is still the same with the website
.....
echo "
    <form action='processupload.php' method='post' enctype='multipart/form-data' id=MyUploadForm>
    <input name='FileInput' id='FileInput' type='file' />
    <input type='submit'  id='submit-btn' value='Upload' />
    <img src='images/ajax-loader.gif' id='loading-img' style='display:none;' alt='Please Wait'/>
    </form>
    <div id='progressbox' ><div id='progressbar'></div ><div id='statustxt'>0%</div></div>
    <div id='output'></div>
  ";

processupload.php :

<?php

if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
############ Edit settings ##############
//$UploadDirectory  = '/impfile'; //specify upload directory ends with / (slash)
##########################################


//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
    die();
}


//Is file size is less than allowed size.
if ($_FILES["FileInput"]["size"] > 5242880) {
    die("File size is too big!");
}

//allowed file type Server side check
switch(strtolower($_FILES['FileInput']['type']))
    {
        case 'text/plain':
            break;
        default:
            die('Unsupported File!'); //output error
}

$File_Name          = $_FILES['FileInput']['name'];

if(move_uploaded_file($_FILES['FileInput']['tmp_name'], "/impfile/".$File_Name ))
{
    die('Success! File Uploaded.');
}else{
    die('error uploading File!');
}

}
else
{
die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}

The problem is the feedback always showing error

die('error uploading File!');

I think the problem isn't from the code, because I can't found the php.ini in the same path that phpinfo showed me. I already set the folder (impfile) to be writeable too.

Can someone show me where did I do wrong in the code? Or maybe the php.ini? If the php.ini is the problem, how can I add the php.ini? Or maybe there's something else?

Every help would be appreciated. Thank you.

12
  • What permissions did you give the folder? Commented Jun 13, 2014 at 7:36
  • @npfedwards BTW, the path has to be a realpath, as the path is.. he's maybe trying to move the file on the realpath /impfile/ and not a relative one, i suppose that he doesn't want this.. Commented Jun 13, 2014 at 7:38
  • @npfedwards I'm using filezilla and I give 777 as the value. is it wrong? Commented Jun 13, 2014 at 7:40
  • @GotchaRob there is a folder name "impfile" in the same folder with processupload.php Commented Jun 13, 2014 at 7:42
  • did you try to follow more websites' advice on how to upload files before asking here? I've found many sites have false/outdated/server specific information and a mix and match of many gives the best result, especially understanding the underlying technology Commented Jun 13, 2014 at 7:43

1 Answer 1

2

Try write impfile/ without first slash. This could be help if the script or directory place in nonroot directory of domain.

Try use is_uploaded_file($_FILES['FileInput']['tmp_name']) or/and $_FILES['FileInput']['size']>0 conditions for additional check.

Sign up to request clarification or add additional context in comments.

1 Comment

how stupid i am! Of course it's not a nonroot directory. Thx a lot CnapoB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.