Ajax values with form, and called variables in PHP

Hi, I cannot fathom for the life of me what is wrong with my code. In any case, I believe the ajax code is right (I could be wrong though).

I have a “form” which is one field long and which I don’t care about submitting. I need to use the CURRENT typed in value, so that I can send it over to another page without submitting any values and thus changing page - hence the use of ajax. I have a fake submit button (created by divs) which is outside of the form. I will use that one to submit the content along with the folder name.

On the second page, which is an upload page, I am trying to create a folder. Basically, in my images folder, I want to create ONE subfolder only. So in my form, the person types in a name in the text box, and then that value is going to become the name of the new folder.

I looked on the net concerning something called folder recursion. Someone suggested something about that (but I had misunderstood his suggestion). Anyhow, I have looked online for that to begin with, but without any luck. As I need to use typed in values, what I found online does not help me in that regards.

The best I can output is $folder which is created in my images directory. In any case, since that attempt has failed, I have reworked my coding several times. But the following is the best I can come up with.

Here is my coding:

[code]


<title>File Upload!</title>
<script src="jquery.js"></script>
<script src="javascript.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="pacifico.css" />


</head>
<body>

<script>
setInterval(function(){
     $.post("test.php",{
            value: $("#folder").val()
        }, 
        function(result){
            console.log('saved!');
      });
}, 2000);
</script>


    <div>
    <form>
    	<input type="text" id="folder" name="folder">
        <input id="btn_send" type="button" value="Submit" />
     </form>
    </div>
  
<div class="content">
	<div id="drop-files" ondragover="return false">
		Drop Images Here
	</div>
	
	<div id="uploaded-holder">
		<div id="dropped-files">
			<div id="upload-button">
				<a href="#" class="upload">Upload!</a>
				<a href="#" class="delete">delete</a>
				<span>0 Files</span>
			</div>
		</div>
		<div id="extra-files">
			<div class="number">
				0
			</div>
			<div id="file-list">
				<ul></ul>
			</div>
		</div>
	</div>
	
	<div id="loading">
		<div id="loading-bar">
			<div class="loading-color"> </div>
		</div>
		<div id="loading-content">Uploading file.jpg</div>
	</div>
	
	<div id="file-name-holder">
		<ul id="uploaded-files">
			<h1>Uploaded Files</h1>
		</ul>
	</div>
</div>
</body>
</html>

[/code]

and the other page

[php]<?php

$folder = $_GET['folder'] . '/'; 
 
 
$dirPath = $folder; 
 
$result = mkdir($dirPath, 0755); 
if ($result == 1) { 
    echo $dirPath . " has been created"; 
} else { 
    echo $dirPath . " has NOT been created"; 
} 
 
 
// We're putting all our files in a directory called images. 
$uploaddir = 'images/'.'$folder'; 
 
// The posted data, for reference 
$file = $_POST['value']; 
$name = $_POST['name']; 
 
// Get the mime 
$getMime = explode('.', $name); 
$mime = end($getMime); 
 
// Separate out the data 
$data = explode(',', $file); 
 
// Encode it correctly 
$encodedData = str_replace(' ','+',$data[1]); 
$decodedData = base64_decode($encodedData); 
 
 
if(file_put_contents($uploaddir.$folder.$name, $decodedData)) { 
; 
} 
else { 
    // Show an error message should something go wrong. 
    echo "Something went wrong. Check that the file isn't corrupted"; 
} 
 
?>[/php]

It’s rather confusing as to what you are actually trying to do. You ask about creating a folder but you code also is trying to do file manipulation and putting contents. Uploading files via javascript is far more complicated than the code you have going on now, I would suggest something like uploadify to handle that aspect. The folder thing is fairly easy though.

Not sure why you are using setInterval for the folder stuff. This would be more correct.

$(document).ready(function(){
  $('#btn_send').click(function(){
    var folder = $('#folder').val();
    $.post('test.php', {folder:folder}, function(data){
      console.log(data + 'saved!');
    });
  });
});

Then in your processing file test.php, you had it set to $_GET instead of $_POST.
[php]
$folder = $_POST[‘folder’] . ‘/’;

$dirPath = $folder; 
 
$result = mkdir($dirPath, 0755); 
if ($result == 1) { 
    echo $dirPath . " has been created"; 
} else { 
    echo $dirPath . " has NOT been created"; 
}

[/php]

Well, it would be for multiple people uploading files to a created folder, where the name would be determined by them. Thus, multiple folders would be created.

Concerning uploadify, I’ll have to read more about it before purchasing it (as I’m not even home right now).

In any case, thank you for your help.

Uploadify is free. It doesn’t matter how many people are using the form, the code I gave should work fine.

It does create a folder, but the uploaded images for example, are not saved at all. Or at best, they would be saved in the images folder at best (which is a big no no).

This is the error code I get.

testing_folder/ has been created Warning: file_put_contents(images/$foldertesting_folder/) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Is a directory in /home/***/***/***/upload.php on line35 Something went wrong. Check that the file isn't corruptedsaved!

So, I modified the coding which now looks like this:

[php]<?php

$folder = $_POST['folder'] . '/'; 
 
 
$dirPath = $folder; 
 
$result = mkdir($dirPath, 0755); 
if ($result == 1) { 
    echo $dirPath . " has been created"; 
} else { 
    echo $dirPath . " has NOT been created"; 
} 
 
 
// We're putting all our files in a directory called images. 
$uploaddir = 'images/'; 
 
// The posted data, for reference 
$file = $_POST['value']; 
$name = $_POST['name']; 
 
// Get the mime 
$getMime = explode('.', $name); 
$mime = end($getMime); 
 
// Separate out the data 
$data = explode(',', $file); 
 
// Encode it correctly 
$encodedData = str_replace(' ','+',$data[1]); 
$decodedData = base64_decode($encodedData); 
 
 
if(file_put_contents($uploaddir.$name, $decodedData)) { 
; 
} 
else { 
    // Show an error message should something go wrong. 
    echo "Something went wrong. Check that the file isn't corrupted"; 
} 
 
?>

[/php]

Now it gives me this error (while saving the uploads in the images folder)

testing_folder/ has been created Warning: file_put_contents(images/) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Is a directory in/home/***/***/***/upload.php on line 35 Something went wrong. Check that the file isn't corruptedsaved!

Btw, thank you for correcting the part that was wrong. Oh, and I must of gone onto the wrong uploadify page.

… this problem I’m having stumps me -_-

The saving of the images won’t work as I stated before. It’s far more complicated than you have setup do do that with javascript, hence the uploadify scenario.

My bad. I misunderstood your earlier comment. I will take a look at uploadify for sure. Maybe it’ll help me with the precise idea I have.

Sponsor our Newsletter | Privacy Policy | Terms of Service