YUI Uploader ... Can't get it to upload

n00b0101
n00b0101 used Ask the Experts™
on
Trying to implement the example of the YUI uploader, but even though it says the file has been successfully uploaded, it's nowhere to be found... I'm basically using the single file upload example as is, except that I changed uploader.upload to uploader.upload(fileID, "/up.php")...  

When I return the $_POST and $_FILES contents, I get this:

Array
(
    [Filename] => aa024380_uc.jpg
    [var2] => var2 default value
    [var1] => var1 default value
    [Upload] => Submit Query
)
Array
(
    [Filedata] => Array
        (
            [name] => aa024380_uc.jpg
            [type] =>
            [tmp_name] =>
            [error] => 6
            [size] => 0
        )

)


So, the tmp_name is blank, which I was thinking maybe was the issue, although I can't figure out why...  

The scripts that I'm including (maybe I left something out?) are as follows:

<script type="text/javascript" src="/js/yahoo-min.js"></script>
<script type="text/javascript" src="/js/utilities.js"></script>
<script type="text/javascript" src="/js/yahoo-dom-event.js"></script>
<script type="text/javascript" src="/js/element-min.js"></script>
<script type="text/javascript" src="/js/uploader.js"></script>

The PHP script looks like this:

(I tried with $file['tmp_name'], but since it's blank...)

<?php
 
set_time_limit(300);
header('Content-type: text/json');
error_reporting(E_ALL);
 
if(isset($_FILES['Filedata']))
{
	$file = $_FILES['Filedata'];
	$name = $file['name'];
	
	move_uploaded_file($file['name'], "/images/".$file['name']);
	
	print_r($_POST);
	print_r($_FILES);
	exit();
}
 
?>
 
 
// YUI SCRIPT BELOW
 
 
 
// Upload Script (with the form) 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 
 
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title></title>
<script type="text/javascript" src="/js/yahoo-min.js"></script>
<script type="text/javascript" src="/js/utilities.js"></script>
<script type="text/javascript" src="/js/yahoo-dom-event.js"></script>
<script type="text/javascript" src="/js/element-min.js"></script>
<script type="text/javascript" src="/js/uploader.js"></script>
 
<!--there is no custom header content for this example-->
 
</head>
<body>
 
 
 
<style>
#selectFilesLink a, #uploadFilesLink a, #clearFilesLink a {
	color: #0000CC;
	background-color: #FFFFFF;
}
 
#selectFilesLink a:visited, #uploadFilesLink a:visited, #clearFilesLink a:visited {
	color: #0000CC;
	background-color: #FFFFFF;
}
 
#uploadFilesLink a:hover, #clearFilesLink a:hover {	
	color: #FFFFFF;
	background-color: #000000;
}
</style>
 
<div id="uiElements" style="display:inline;">
		<div id="postVars">
		Set custom values for a couple POST vars:<br/>
		var1: <input type="text" id="var1Value" value="var1 default value" /><br/>
		var2: <input type="text" id="var2Value" value="var2 default value" /><br/><br/>
		</div>
 
		<div id="uploaderContainer">
			<div id="uploaderOverlay" style="position:absolute; z-index:2"></div>
			<div id="selectFilesLink" style="z-index:1"><a id="selectLink" href="#">Select File</a></div>
		</div>
		<div id="uploadFilesLink"><a id="uploadLink" onClick="upload(); return false;" href="#">Upload File</a></div><br/>
		<div id="selectedFileDisplay">
		Progress: <input type="text" cols="50" id="progressReport" value="" readonly /><br/><br/>
 
		</div>
		<div id="returnedDataDisplay">
		Data returned by the server:<br/>
		<textarea id="serverData" rows="5" cols="50"></textarea>
		</div>
</div>
 
 
<script type="text/javascript">
 
YAHOO.util.Event.onDOMReady(function () { 
var uiLayer = YAHOO.util.Dom.getRegion('selectLink');
var overlay = YAHOO.util.Dom.get('uploaderOverlay');
YAHOO.util.Dom.setStyle(overlay, 'width', uiLayer.right-uiLayer.left + "px");
YAHOO.util.Dom.setStyle(overlay, 'height', uiLayer.bottom-uiLayer.top + "px");
});
 
	// Custom URL for the uploader swf file (same folder).
	YAHOO.widget.Uploader.SWFURL = "/js/assets/uploader.swf";
 
    // Instantiate the uploader and write it to its placeholder div.
	var uploader = new YAHOO.widget.Uploader( "uploaderOverlay" );
	
	// Add event listeners to various events on the uploader.
	// Methods on the uploader should only be called once the 
	// contentReady event has fired.
	
	uploader.addListener('contentReady', handleContentReady);
	uploader.addListener('fileSelect', onFileSelect)
	uploader.addListener('uploadStart', onUploadStart);
	uploader.addListener('uploadProgress', onUploadProgress);
	uploader.addListener('uploadCancel', onUploadCancel);
	uploader.addListener('uploadComplete', onUploadComplete);
	uploader.addListener('uploadCompleteData', onUploadResponse);
	uploader.addListener('uploadError', onUploadError);
    uploader.addListener('rollOver', handleRollOver);
    uploader.addListener('rollOut', handleRollOut);
    uploader.addListener('click', handleClick);
    	
    // Variable for holding the selected file id.
	var fileID;
	
	// When the mouse rolls over the uploader, this function
	// is called in response to the rollOver event.
	// It changes the appearance of the UI element below the Flash overlay.
	function handleRollOver () {
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#FFFFFF");
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#000000");
	}
	
	// On rollOut event, this function is called, which changes the appearance of the
	// UI element below the Flash layer back to its original state.
	function handleRollOut () {
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'color', "#0000CC");
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get('selectLink'), 'background-color', "#FFFFFF");
	}
	
	// When the Flash layer is clicked, the "Browse" dialog is invoked.
	// The click event handler allows you to do something else if you need to.
	function handleClick () {
	}
	
	// When contentReady event is fired, you can call methods on the uploader.
	function handleContentReady () {
	    // Allows the uploader to send log messages to trace, as well as to YAHOO.log
		uploader.setAllowLogging(true);
		
		// Disallows multiple file selection in "Browse" dialog.
		uploader.setAllowMultipleFiles(false);
		
		// New set of file filters.
		var ff = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
		                   {description:"Videos", extensions:"*.avi;*.mov;*.mpg;*.flv;*.swf"});
		                   
		// Apply new set of file filters to the uploader.
		uploader.setFileFilters(ff);
	}
 
	// Actually uploads the files. Since we are only allowing one file
	// to be selected, we use the upload function, in conjunction with the id 
	// of the selected file (returned by the fileSelect event). We are also including
	// the text of the variables specified by the user in the input UI.
 
	function upload() {
	if (fileID != null) {
		uploader.upload(fileID, "/admin/up.php", 
		                "POST", 
		                {var1:document.getElementById("var1Value").value,
						 var2:document.getElementById("var2Value").value});
	}	
	}
	
	// Fired when the user selects files in the "Browse" dialog
	// and clicks "Ok". Here, we set the value of the progress
	// report textfield to reflect the fact that a file has been
	// selected.
	
	function onFileSelect(event) {
		for (var file in event.fileList) {
		    if(YAHOO.lang.hasOwnProperty(event.fileList, file)) {
				fileID = event.fileList[file].id;
			}
		}
		
		this.progressReport = document.getElementById("progressReport");
		this.progressReport.value = "Selected " + event.fileList[fileID].name;
	}
 
 
    // When the upload starts, we inform the user about it via
	// the progress report textfield. 
	function onUploadStart(event) {
		this.progressReport.value = "Starting upload...";
	}
	
	// As upload progresses, we report back to the user via the
	// progress report textfield.
	function onUploadProgress(event) {
		prog = Math.round(100*(event["bytesLoaded"]/event["bytesTotal"]));
		this.progressReport.value = prog + "% uploaded...";
	}
	
	// Report back to the user that the upload has completed.
	function onUploadComplete(event) {
		this.progressReport.value = "Upload complete.";
	}
	
	// Report back to the user if there has been an error.
	function onUploadError(event) {
		this.progressReport.value = "Upload error.";
	}
	
	// Do something if an upload is canceled.
	function onUploadCancel(event) {
 
	}
	
	// When the data is received back from the server, display it to the user
	// in the server data text area.
	function onUploadResponse(event) {
		this.serverData = document.getElementById("serverData");
		this.serverData.value = event.data;
	}
</script>
 
</body>
</html>
 
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I see a couple of problems in the script:

Line 12 has move_uploaded_file($file['name'], "/images/".$file['name']);
2 problems here.  You need to use $file['tmp_name'] for the first variable here and the second variable has to be an absolute value not a relative one ("/home/httpdocs/images/".$file['name']) otherwise the file won't end up anywhere.

Make sure that your tmp directory is writable by PHP.  You can check this using phpinfo() and finding your tmp path and making sure that the permissions are correct.  It might not be.  I would also check to see if you have any open_basedir() (restricts the sites ability to see only specific directories) restrictions also.

I would also change line 12 to the code example below (This checks to see if the file is an uploaded file as far as PHP is concerned and if it can be moved:
<?php
 
//12. move_uploaded_file($file['name'], "/images/".$file['name']);
if (is_uploaded_file($file['tmp_name']) ) {
    if( move_uploaded_file($file['tmp_name'], "/images/".$file['name']) ) {
        echo 'There was a problem moving the file';
    } else {
        echo 'File was not uploaded';
    }
} else {
    echo 'File was not uploaded';
}

Open in new window

Author

Commented:
Thanks for the response...

I changed it to an absolute path and put your if statement in there.  It returns false for is_uploaded_file.  What does that mean?  
That means that your temp directory is most likely not writable by PHP.  Double check the location of your temp directory and make sure it is writable.

Author

Commented:
Thanks!  Your smartness is appreciated :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial