• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 591
  • Last Modified:

Edit upload form to include SWF file, embed SWF on dynamic page in Dreamweaver

I need to extend my current file upload web form to allow for the inclusion of SWF files. The form and PHP script included here successfully uploads JPEG's-the name to a database and the JPEG to a folder on the server. Dreamweaver then creates dynamic pages on-the-fly which include the pictures and text I've uploaded with this form.
I need an edit for this script and form that will include the ability to upload a SWF file and likewise sent its name to a database in the file to a folder. I would then subsequently need a procedure for Dreamweaver by which dynamically generated pages will include the uploaded SWF files  the same way in which they had embeded the text and pictures ( I do this by including a src attribute to the binding for the image file in the mysql record set) .
An example of the type of page I'm looking to produce is here... note the flash slideshow and other dynamic content:
http://www.cityhummer.com/vehicle.asp?v=781488
Thank you.
<form enctype="multipart/form-data" action="add.php" method="POST"> 
Name: <input type="text" name="name"><br> 
E-mail: <input type="text" name = "email"><br> 
Phone: <input type="text" name = "phone"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form>


<?php 

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$ID=$_POST['ID'];
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 

// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
mysql_select_db("employees") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$ID','$name', '$email', '$phone', 'images/$pic')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?>

Open in new window

0
trptdblc
Asked:
trptdblc
  • 7
  • 5
1 Solution
 
v2MediaCommented:
I see nothing in the code above that would stop any file type being uploaded.

As far as displaying the swf or jpg image in subsequent pages is concerned, you could conditionally display an image or embedded object (flash) based on the string "images/$pic".

On the page that displays the images, you would insert a flash object as you would normally. Then as the mysql query is outputted to the browser, do a string search for 'swf'. If found, display the flash object, else display an image.

Example code below:
<?php
//untested code
//mysql query/result here
$pic_path = $row['image'];

//preg_match will return 1 if pic is a swf file
$search_swf = preg_match('/swf/i',$pic_path);
$is_flash = ($search_swf > 0) ? true : false;

if(!$is_flash) { ?>

<!-- image code here -->

<?php } else { ?>

<!-- flash code here -->

<?php } ?>

Open in new window

0
 
Jason C. LevineNo oneCommented:
v2,

He's building off this post:

http://www.experts-exchange.com/Web_Development/Software/Macromedia_Dreamweaver/Q_25027884.html

and probably needs to store just the filename without the .swf extension to implement the above...
0
 
v2MediaCommented:
@J: If the filename is stored without the extension, then an additional field would need to be added to the db table for file format and several mods made to his script. It would be easier to use the script and db as is and have the dynamic page do a string search at runtime to determine format and method of output to html.
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
trptdblcAuthor Commented:
Thank you,
can you suggest where in my existing code your reccomended code should be placed?
0
 
v2MediaCommented:
It wouldn't be placed in the script you posted; the tweak would apply to the page viewing the img/swf's.
0
 
trptdblcAuthor Commented:
I am awardig you the ponts. I know little about coding so it may take forever to get this to work. My grades are for your promptness and obvious expertise. If you get a munuite and can explain all this more simply or could suggest a book or other resourse,great .If not thanks anyway
0
 
v2MediaCommented:
If you post the code for your page that displays the image/s, I'll work the solution into it with explanatory notes. I'd also need the structure of `employees` database. You can get the structure with the code below:-

<?php
//obviously fill in the db user/pass
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("employees") or die(mysql_error());
$result = mysql_query("DESC employees");
$data = mysql_fetch_assoc($result);
echo '<pre>';
print_r($data);
echo '</pre>';
?>

Open in new window

0
 
trptdblcAuthor Commented:
I was able to get this to work myself. Thank you . If possible, the form and it's code included at the top of this post needs an edit to allow the file uload of a swf as well as a jpeg. I can revise the mysql to accept new data ad put a new folder on the server labled flash. If not, thank you anyway.
0
 
v2MediaCommented:
The tweak below incorporates a string search of $pic for the file format 'swf' and if found, will toggle the upload folder to "flash", else the upload folder will be "images". It also sanitises form data and file name.
<?php 
//This gets all the other information from the form
if(!empty($_POST['name'])) {
	
	$ID = $_POST['ID'];
	$name = (!get_magic_quotes_gpc()) ? mysql_real_escape_string($_POST['name']) : $_POST['name']; 
	$email = (!get_magic_quotes_gpc()) ? mysql_real_escape_string($_POST['email']) : $_POST['email']; 
	$phone = (!get_magic_quotes_gpc()) ? mysql_real_escape_string($_POST['phone']) : $_POST['phone']; 
	$pic = ($_FILES['photo']['name']);
	
	//filename to lowercase
	$new_pic = strtolower($pic);
	
	//replace space chars
	$new_pic = str_replace(' ','_',$new_pic);
	
	//strip non alphanumeric characters
	$pattern = "/[^a-z0-9_]/";
	$new_pic = preg_replace($pattern,'',$new_pic);
	
	//preg_match will return 1 if pic is a swf file
	$search_swf = preg_match('/swf/',$new_pic);
	$is_flash = ($search_swf > 0) ? true : false;
	
	//This is the directory where images will be saved 
	$target = ($is_flash) ? "flash/" : "images/"; 
	$target .= $new_pic;
	
	//Writes the photo to the server 
	if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 
	
		// Connects to your Database 
		mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
		mysql_select_db("employees") or die(mysql_error()) ; 
		
		//Writes the information to the database 
		mysql_query("INSERT INTO `employees` VALUES ('$ID','$name', '$email', '$phone', $target)") ; 
		
		//Tells you if its all ok 
		echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
		
	} else { 
	
		//Gives and error if its not 
		echo "Sorry, there was a problem uploading your file. Try using a smaller file size.";
		 
	}
} 
?>
<form enctype="multipart/form-data" action="add.php" method="POST"> 
Name: <input type="text" name="name"><br> 
E-mail: <input type="text" name = "email"><br> 
Phone: <input type="text" name = "phone"><br> 
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form>

Open in new window

0
 
trptdblcAuthor Commented:
To Clarify: I need to upload the file names of a swf and jpeg simultaneously on the same form to seperate fields in mysql and the file themseves to folders on the server...is it a simple mater to just add a second file field to the form? And, the php code must have a prefix for each field they are written to, eg: flash/$flash, images/$images (This will allow a full path to the files to be atached to files on dynamic pages)
Thanks again...
0
 
v2MediaCommented:
It will do as you describe for a single file upload only. Sorry, but I'm not prepared to code up a multi-file upload form, it takes too much time.
0
 
v2MediaCommented:
Open another question if you'd like additional answers.
0
 
trptdblcAuthor Commented:
Will do
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now