php/ ajax uploader problem

file permission possible problem

Page 1 of 1

5 Replies - 2578 Views - Last Post: 29 March 2009 - 07:45 AM Rate Topic: -----

#1 hacked_the_gibson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-March 09

php/ ajax uploader problem

Post icon  Posted 27 March 2009 - 03:37 PM

Hi there,

I am currently developing a ajax uploader with php on the back end. Just wondering if anyone has came up with this problem.

This is using the YUI java library and im going crazy becuase i have tested my php script and it works fine with file permissions of 777 for the upload directory and 644 on the uploader.php

If the file permissions are wrong let me know, i have a feeling my file permissions are set-up wrong.

here is a simpler form of the code......

HTML script:

<!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>Simple Uploader Example With Button UI</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
	margin:0;
	padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/fonts/fonts-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script>

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/uploader/uploader.js"></script>

<!--there is no custom header content for this example-->

</head>

<body class=" yui-skin-sam">


<h1>Simple Uploader Example With Button UI</h1>

<div class="exampleIntro">
	<p>This example is a demonstration of the <a href="../../uploader/">YUI Uploader Control</a>'s features.</p>

<p><strong>Note:</strong> The YUI Uploader Control requires Flash Player 9.0.45 or higher. The latest version of Flash Player is available at the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</p>
<p><strong>Note:</strong> The YUI Uploader Control requires the uploader.swf Flash file that is distributed as part of the YUI package, in the uploader/assets folder. Copy the uploader.swf to your server and set the YAHOO.Uploader.SWFURL variable to its full path.</p>
<p><strong>Note:</strong> This example uses a server-side script to accept file uploads. The script used does not open or store any data sent to it. Nonetheless, when trying out the example, do not send any sensitive or private data. Do not exceed file size of 10 MB.			
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->

<style type="text/css">
	.uploadButton a, .clearButton a {
		display:block;
		width:100px;
		height:40px;
		text-decoration: none;
		margin-left:5px;
	}
	
	.uploadButton a {
		background: url("assets/uploadFileButton.png") 0 0 no-repeat;
	}
	
	.clearButton a {
		background: url("assets/clearListButton.png") 0 0 no-repeat;
	}
	
	.uploadButton a:visited, .clearButton a:visited {
		background-position: 0 0;
	}
	
	.uploadButton a:hover, .clearButton a:hover {	
		background-position: 0 -40px;
	}
	
	.uploadButton a:active, .clearButton a:active {
		background-position: 0 -80px;
	}
</style>

<div>
	<div id="fileProgress" style="border: black 1px solid; width:300px; height:40px;float:left">
		<div id="fileName" style="text-align:center; margin:5px; font-size:15px; width:290px; height:25px; overflow:hidden"></div>
		<div id="progressBar" style="width:300px;height:5px;background-color:#CCCCCC"></div>
	</div>
<div id="uploaderUI" style="width:100px;height:40px;margin-left:5px;float:left"></div>
<div class="uploadButton" style="float:left"><a class="rolloverButton" href="#" onclick="upload(); return false;"></a></div>
<div class="clearButton" style="float:left"><a class="rolloverButton" href="#" onclick="handleClearFiles(); return false;"></a></div>
</div>


<script type="text/javascript">
	
	// Instantiate the uploader and write it to its placeholder div.
	
	YAHOO.widget.Uploader.SWFURL = "assets/uploader.swf";
	
	var uploader = new YAHOO.widget.Uploader( "uploaderUI", "assets/selectFileButton.png" );
	
	// 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);
		
	// Variable for holding the selected file ID.
	var fileID;
	
	function handleClearFiles() {
	uploader.clearFileList();
	uploader.enable();
	fileID = null;
	
	var filename = document.getElementById("fileName");
	filename.innerHTML = "";
	
	var progressbar = document.getElementById("progressBar");
	progressbar.innerHTML = "";
	}
		
	// 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);
		
		// Restrict selection to a single file (that's what it is by default,
		// just demonstrating how).
		uploader.setAllowMultipleFiles(false);
		
		// New set of file filters.
		var ff = new Array({description:"Images", extensions:"*.jpg;*.png;*.gif"},
						   {description:"Videos", extensions:"*.avi;*.mov;*.mpg"});
						   
		// Apply new set of file filters to the uploader.
		uploader.setFileFilters(ff);
	}

	// Initiate the file upload. Since there's only one file, 
	// we can use either upload() or uploadAll() call. fileList 
	// needs to have been populated by the user.
	function upload() {
	if (fileID != null) {
		uploader.upload(fileID, "uploader.php");
		fileID = null;
	}
	}
	
	// Fired when the user selects files in the "Browse" dialog
	// and clicks "Ok".
	function onFileSelect(event) {
		for (var item in event.fileList) {
			if(YAHOO.lang.hasOwnProperty(event.fileList, item)) {
				YAHOO.log(event.fileList[item].id);
				fileID = event.fileList[item].id;
			}
		}
		uploader.disable();
		
		var filename = document.getElementById("fileName");
		filename.innerHTML = event.fileList[fileID].name;
		
		var progressbar = document.getElementById("progressBar");
		progressbar.innerHTML = "";
	}

	// Do something on each file's upload start.
	function onUploadStart(event) {
	
	}
	
	// Do something on each file's upload progress event.
	function onUploadProgress(event) {
		prog = Math.round(300*(event["bytesLoaded"]/event["bytesTotal"]));
		  progbar = "<div style=\"background-color: #f00; height: 5px; width: " + prog + "px\"/>";

		var progressbar = document.getElementById("progressBar");
		progressbar.innerHTML = progbar;
	}
	
	// Do something when each file's upload is complete.
	function onUploadComplete(event) {
		uploader.clearFileList();
		uploader.enable();
		
		progbar = "<div style=\"background-color: #f00; height: 5px; width: 300px\"/>";
		var progressbar = document.getElementById("progressBar");
		progressbar.innerHTML = progbar;
	}
	
	// Do something if a file upload throws an error.
	// (When uploadAll() is used, the Uploader will
	// attempt to continue uploading.
	function onUploadError(event) {

	}
	
	// Do something if an upload is cancelled.
	function onUploadCancel(event) {

	}
	
	// Do something when data is received back from the server.
	function onUploadResponse(event) {
		YAHOO.log("Server response received.");
	}

</script>

</body>
</html>
 



PHP uploader.php

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
	{
	echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	}
  else
	{
	
	if (file_exists("upload/" . $_FILES["file"]["name"]))
	  {
	  echo $_FILES["file"]["name"] . " already exists. ";
	  }
	else
	  {
	  move_uploaded_file($_FILES["file"]["tmp_name"],
	  "upload/" . $_FILES["file"]["name"]);
	  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
	  }
	}
  }
else
  {
  echo "Invalid file";
  }
?>





all of the files and folders are in the same directory. Also i have tried on the htm page referencing the uploader.php as a direct reference too . so http://www.example.com/uploader.php


Still nothing.....

im annoyed with myself becuase i had this working last week but i changed alot and forgot where and what i did :P

if i find out the problem ill let you know. try and save some one else hours of fun debugging

Cheers,

Mike

Is This A Good Question/Topic? 0
  • +

Replies To: php/ ajax uploader problem

#2 vb6_god  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 03-December 07

Re: php/ ajax uploader problem

Posted 27 March 2009 - 10:45 PM

Any one got any ideas on file permissions ?????
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: php/ ajax uploader problem

Posted 28 March 2009 - 06:44 AM

It's not clear what the behavior is here as you've not provided any error messages or anything. You specifically says it's working:

Quote

This is using the YUI java library and im going crazy becuase i have tested my php script and it works fine with file permissions of 777 for the upload directory and 644 on the uploader.php

If the file permissions are wrong let me know, i have a feeling my file permissions are set-up wrong.

Did you change the permissions from what you've said here?
Who is the owner and what are the permissions of both the temporary directory and the uploads directory?

You're not checking the return value of move_uploaded_file(), which will also issue a warning on permissions failure.
Was This Post Helpful? 0
  • +
  • -

#4 hacked_the_gibson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-March 09

Re: php/ ajax uploader problem

Posted 28 March 2009 - 09:44 PM

View PostJackOfAllTrades, on 28 Mar, 2009 - 05:44 AM, said:

It's not clear what the behavior is here as you've not provided any error messages or anything. You specifically says it's working:

Quote

This is using the YUI java library and im going crazy becuase i have tested my php script and it works fine with file permissions of 777 for the upload directory and 644 on the uploader.php

If the file permissions are wrong let me know, i have a feeling my file permissions are set-up wrong.

Did you change the permissions from what you've said here?
Who is the owner and what are the permissions of both the temporary directory and the uploads directory?

You're not checking the return value of move_uploaded_file(), which will also issue a warning on permissions failure.


Well basically i dont get anything....

Say i upload an image, it will show the loading bar as if it uploaded the file but nothing shows up in the directory. So i assume the permissions are wrong.

the upload directory is 777
the temp directory hasn't changed it's 755
Was This Post Helpful? 0
  • +
  • -

#5 Gevie  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 06-April 08

Re: php/ ajax uploader problem

Posted 28 March 2009 - 11:02 PM

Does the SWF file have correct permissions?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: php/ ajax uploader problem

Posted 29 March 2009 - 07:45 AM

Which user owns the directories? Have you turned error logging up and/or on? If so, what is it telling you? Is there evidence in your web server log files that the file was actually transmitted?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1