How to send file information when uploading img via ajax

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 588 Views - Last Post: 09 May 2019 - 03:05 PM Rate Topic: -----

#16 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: How to send file information when uploading img via ajax

Posted 09 May 2019 - 11:28 AM

Just add some output to have it tell you what it's doing. For every if/else, have it print something saying what it's doing so you can follow it.

It's unusual to define a function inside a try block, but I doubt that would cause a problem. Just be aware that the try block only catches an exception when the function is defined, not when it's executed. If you want to catch an exception when it's executed then put the function call inside a try/catch block.
Was This Post Helpful? 0
  • +
  • -

#17 pfar54   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 30-April 15

Re: How to send file information when uploading img via ajax

Posted 09 May 2019 - 12:40 PM

Well the thing is, none of my var_dumps are working in the code below. Therefore I think part of the issue is the code in the UploadFile function isn't working.

I took out
$p_img = $_FILES['file'];


Do I need the $_FILES[] somewhere in the code to get the file that is being sent over via formData in the ajax?

$p_name = trim(htmlspecialchars($_POST['p_name'], ENT_QUOTES));
$p_alt = trim(htmlspecialchars($_POST['p_alt']));
$category = trim(htmlspecialchars($_POST['categoryName']));
$creator = trim(htmlspecialchars($_POST['creatorName']));
$status = $_POST['status'];

	function UploadFile($fileArray, $destinationFolder = '../project_images/') {
		//$p_img = $fileArray['file'];

		$fileUploadData = $fileArray['file'];
		var_dump($fileUploadData);

Was This Post Helpful? 0
  • +
  • -

#18 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: How to send file information when uploading img via ajax

Posted 09 May 2019 - 12:42 PM

Quote

Well the thing is, none of my var_dumps are working in the code below.

Nothing that you can do will break var_dump. It will never "not work." If it's not outputting anything, then that line of code is not even being executed at all. So, how do you figure out why it's not executing? Is the function call inside an if statement? Have you verified that the if statement is actually true or are you just assuming that it's true? This is why you add output to have PHP tell you what it's doing.
Was This Post Helpful? 0
  • +
  • -

#19 pfar54   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 30-April 15

Re: How to send file information when uploading img via ajax

Posted 09 May 2019 - 12:52 PM

There is a call at the very end in the if isset.

I know this is wrong, but am not sure how to call the function property. Any help would be appreciated to help me learn.

function UploadFile($fileArray, $destinationFolder = '../project_images/') {
		//$p_img = $fileArray['file'];

		$fileUploadData = $fileArray['file'];
		var_dump($fileUploadData);
		$filename       =   $fileUploadData['name'];
		$tmp_name       =   $fileUploadData['tmp_name'];
		$filesize       =   $fileUploadData['size'];
		$file_error     =   $fileUploadData['error'];
       /* $file           =   $fileArray['file'];*/
		
		/*
		$filename       =   $fileArray[$fileNameVar];
        $tmp_name       =   $fileArray[$fileTmpNameVar];
        $filesize       =   $fileArray[$fileSizeVar];
        $file_error     =   $fileArray[$fileErrorVar];
        $file           =   $fileArray[$p_img];
*/
		var_dump($filename);
        // Save all the default data.
        // Success and error should be set by default to fail
        $return['error']        =   true;
        $return['success']      =   false;
        $return['file']['dest'] =   $destinationFolder.$filename;
        $return['file']['size'] =   $filesize;

        if($file_error == 0)
            $return['error']    =   false;
        // I added a directory creation function so you don't have to 
        // manually make folders. This will do it for you.
        if(!is_dir($destinationFolder))
            mkdir($destinationFolder,0755,true);
        // If your filename is not empty, return success or fail of upload
        if (!empty($filename))
            $return['success']  = (move_uploaded_file($tmp_name, $destinationFolder.$filename));

        return $return;
    }
	if (UploadFile	== true) {
		return UploadFile;
	} else {
		var_dump("UploadFile Failed");
	}

Was This Post Helpful? 0
  • +
  • -

#20 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2290
  • View blog
  • Posts: 6,981
  • Joined: 15-January 14

Re: How to send file information when uploading img via ajax

Posted 09 May 2019 - 03:05 PM

You have that function returning an array with success, error, etc. Check what it returned.

$result = UploadFile($_FILES);
print_r($result);


I'm not sure if you don't understand when I say you need to have it tell you what it's doing, but I'm talking about stuff like this:

if(isset($_POST['create'])) {
  echo 'Line ' . __LINE__ . ', calling UploadFile<br>';

  // Try uploading
  $upload =   UploadFile($_FILES);

  ...
}
else {
  echo 'Line ' . __LINE__ . ', $_POST["create"] was not set.  $_POST: ' . print_r($_POST, true) . '<br>';
}


Literally make it tell you what it's doing. Print out variables so you can check them. Whenever there is an if statement, make it tell you what it decided to do. Make it tell you whatever you need to know to understand what it is doing and why it is doing it.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2