jquery upload file pass php variable to Javascript

Asked

Viewed 416 times

0

am using this plugin JQUERY UPLOAD FILE and it is all ok with the upload I am increasing function of creating directory and upar inside them however I am not able to pass the variable id of php to the page that makes the service my code is so:

php page

$id = $_GET['id'];
if(is_dir("../imagens/$id")) {
echo "diretorio ja existe";
}
else {
mkdir ("../imagens/$id", 0700 );
echo "diretorio criado";
}

<div id="mulitplefileuploader">Upload</div>

$(document).ready(function()
{
var settings = {
    url: "upload.php?id=<?php echo $id ?>",
    dragDrop:true,
    fileName: "myfile",
    allowedTypes:"jpg,png,gif,doc,pdf,zip",	
    returnType:"json",
	 onSuccess:function(files,data,xhr)
    {
       // alert((data));
    },
    showDelete:true,
    deleteCallback: function(data,pd)
	{
    for(var i=0;i<data.length;i++)
    {
        $.post("uploadDelete.php",{op:"delete",name:data[i]},
        function(resp, textStatus, jqXHR)
        {
            //Show Message  
            $("#status").append("<div>File Deleted</div>");      
        });
     }      
    pd.statusbar.hide(); //You choice to hide/not.

}
}
var uploadObj = $("#mulitplefileuploader").uploadFile(settings);


});

upload.php

$id = $_GET['id'];

$output_dir = "../imagens/$id";
if(isset($_FILES["myfile"]))
{
	$ret = array();

	$error =$_FILES["myfile"]["error"];
	//You need to handle  both cases
	//If Any browser does not support serializing of multiple files using FormData() 
	if(!is_array($_FILES["myfile"]["name"])) //single file
	{
 	 	$fileName = $_FILES["myfile"]["name"];
 		move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
    	$ret[]= $fileName;
	}
	else  //Multiple files, file[]
	{
	  $fileCount = count($_FILES["myfile"]["name"]);
	  for($i=0; $i < $fileCount; $i++)
	  {
	  	$fileName = $_FILES["myfile"]["name"][$i];
		move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
	  	$ret[]= $fileName;
	  }
	
	}
    echo json_encode($ret);
 }

how to pass the variable?

  • Whenever I read Java when someone refers to Javascript, my eyes hurt. :(

1 answer

1


My problem was I was missing a bar:

$output_dir = "../imagens/$id";

Adding her:

$output_dir = "../imagens/$id/";

Worked

  • 1

    I improved the wording of your answer, now you can mark it as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.