Return to Snippet

Revision: 39771
at January 20, 2011 20:45 by danhanly


Initial Code
<?php

require "../../scr/config-data.php.inc";
mysql_connect($host,$username,$password) or die 
("Could Not Connect".mysql_error());
mysql_select_db($db) or die ("Could Not Connect".mysql_error());
$pid = $_POST['pid'];
	
if (!empty($_FILES)) {		
	$selectAlias = mysql_query("SELECT * FROM project WHERE id = '$pid'");
	while($alias_result = mysql_fetch_array($selectAlias)){
		$alias = $alias_result['alias'];		
	}
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];			
	move_uploaded_file($tempFile,$targetFile);
	echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
	
	$filePath = "proj/" .$alias . '-' . $_FILES['Filedata']['name'];
	mysql_query("INSERT INTO image VALUES (NULL, '$pid', '$filePath')") or die("Database Query Error: ". mysql_error());
}

?>

<html>
<head>
<link href="../mod/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../mod/uploadify/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../mod/uploadify/swfobject.js"></script>
<script type="text/javascript" src="../mod/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript">
var pid;

$(document).ready(function() {
	document.getElementById("files").style.display = "none";
	$('#file_upload').uploadify({
	  'uploader'    : '../mod/uploadify/uploadify.swf',
	  'script'      : '../mod/uploadify/uploadify.php',
	  'cancelImg'   : '../mod/uploadify/cancel.png',
	  'folder'      : '../proj',
	  'auto'        : true,
	  'multi'       : true,
	  'onError'     : function (event,ID,fileObj,errorObj) {
      						alert(errorObj.type + ' Error: ' + errorObj.info);
   					  },
	  'onSelectOnce': function(event,data) {
    						$('#file_upload').uploadifySettings('scriptData', {'pid': pid});
  	  				  }
				
	});
});
</script>
<head>
<body>
       <input id="file_upload" name="file_upload" type="file" />
</body>
<html>

Initial URL

                                

Initial Description
The uploaded file is attached to the "pid" of the recently inserted project.

the "pid" variable is retrieved from the database after the project has been entered

Initial Title
Uploadify JQuery & PHP/MySQL Database Control

Initial Tags
mysql, database, php, file, script, jquery

Initial Language
jQuery