Jump to content

Upload Image Changes


wwfc_barmy_army

Recommended Posts

Hello.

 

I have a Jquery image upload script which I found on the internet and i've already made a number of changes to it, although I'm not the best at ajax stuff yet, but I don't know how I can get it to return the random file name created in the php upload script. The image is uploaded with the random file name, but just not returned to the main page yet. Here is my code:

 

In my main page:

<script language="javascript" type="text/javascript">

$(document).ready(function(){
$('#status').hide();
});

$(function(){
var ipup = 0;
var btnUpload=$('#upload');
var status=$('#status');
new AjaxUpload(btnUpload, {
	action: 'upload-file.php',
	//Name of the file input box
	name: 'uploadfile',
	onSubmit: function(file, ext){
		if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                  // check for valid file extension
			status.text('Only JPG, PNG or GIF files are allowed');
			return false;
		}
		status.text('<img src="images/ajax-loader.gif"> Uploading...');
		$('#status').show();

	},
	onComplete: function(file, response){
		//On completion clear the status
		status.text('');
		$('#status').hide();

		//Add uploaded file to list
		if(response==="success"){
			$('<li></li>').appendTo('#files').html('<img src="./images/upload/'+file+'" alt="" /><br />'+file).addClass('success');
			$('<p></p>').appendTo('#files').html('<input name="file' + ipup + '" type="hidden" value="' +file+ '" /><br />');
		ipup = ipup + 1;
		} else{
			$('<li></li>').appendTo('#files').text(file).addClass('error');
		}
	}
});
});


</script>

 

Upload-file.php

<?php
$uploaddir = './images/upload/'; 

  // get the file extension first
$ext = substr(strrchr(basename($_FILES['uploadfile']['name']), "."), 1);

// make the random file name
$randName = md5(rand() * time());

// and now we have the unique file name for the upload file
$file = $uploaddir . $randName . '.' . $ext;

//$result = move_uploaded_file($tmpName, $filePath);



//$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
  echo "success"; 
} else {
echo "error";
}
?>

 

As far as I can see if I echo it the returning value will not be success so will not work, so I'm not sure the best way to go about it.

 

Thanks for any help and advice.  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.