Jump to content

NOOB needs help with upload file


bpburrow

Recommended Posts

The intent of this form is to create a folder for a client and upload an image into another folder 'imagesClients'.  The form works as follows: 

Username:  selected from drop down menu.

Folder:  Slide shows will be FTP to folder.  Repeat clients may have more than one folder. Uploaded images will not go into this folder.

Image:  Selected from window

Active:  Either yes or no

 

mkdir works without any problems.

 

Here are my issues: 

1. image doesn't upload to 'imagesClients' folder.

2. image name isn't uploading to the db.

3. how would I add a file validation to  'if(!$user || !$folder || !$active)'.  I tried creating a variable for 'file' but received an undefined error.

3. I'm self taught through a dummies book, am I doing this right or is there a better method?

 

Thanks in advance!!

 

slideshow_new.php

<?php
session_start();
error_reporting(E_ALL);
require_once('site_fns.php');
include("connect.php");
do_html_header('New Client');
do_menu_main2('');
check_login();
do_mainadmin_menu('');

if(!$_POST['submit']) // 'submit' hasn't been clicked so output html.
{
	//set up form options to select a username	
	$sql = mysql_query("SELECT DISTINCT username FROM Client");
		while($row = mysql_fetch_assoc($sql))
		{
		   $dd .= "<option value='{$row['username']}'>{$row['username']}</option>";
		} 
?>
		<form enctype="multipart/form-data" action="slideshow_new.php" method="post">
			<fieldset>
				<legend>Setup Slideshow</legend>
				<ol>
					<li>Select: <select name="username" style="width: 222px;">
						<? echo $dd; ?></select></li>
					<li>Folder Name: <input type="text" name="foldername" /></li>
					<li><input type="hidden" name="MAX_FILE_SIZE" valude="10000000" />
						Client Image: <input type="file" name="file" id="file"/></li>
					<li>Active Image: <select name="active">
						<option value="yes" selected="selected">Yes</option>
						<option value="no">No</option></select></li>
					<input type="submit" name="submit" value="Add Slideshow" />
				</ol>
			</fieldset>
		</form>
<?php
}
else
{
	$clientusername = protect($_POST['username']);
	$folder = protect($_POST['foldername']);
	$active = protect($_POST['active']);

	$errors = array();

	if(!$user || !$folder || !$active)
	{
		$errors[] = "You did not fill out the required fields.";
	}

	//Ensure client folder doesn't exist.  Creates client folder
	$sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
			$query = mysql_query($sql) or die(mysql_error());

	if(mysql_num_rows($query) > 0) 
	{
		$errors[] = "Folder name is already in use, please try another";
	}
	else
	{
		mkdir("/home/brittao1/public_html/Clients/$folder", 0700);
	}

	//This function will upload your file
	function upload()
	{
		//Collect all info into variables
		$file = $_FILES['file']['name'];
		$type = $_FILES['file']['type'];
		$temp = $_FILES["file"]["tmp_name"];
		$imageSize = getimagesize($temp);

		//If file size is bigger than 1mb display error
		if($imageSize > 1000000)
		{
			$errors[] = "File size must be less than 1MB.";
		}

		//Check allowed file types
		if (preg_match('/image\/(jpg|jpeg|gif|png)/', $imageSize['mime'])) 
		{
			if (file_exists("/home/brittao1/public_html/imagesClients/$file"))
			{
				$errors[] = "$file image already exists.";
		  	}
			else
		  	{			
				//If everything is good then let's upload
				move_uploaded_file($temp, "/home/brittao1/public_html/imagesClients/$file");
			}
			else
			{
				$errors[] = "File type must be .jpg, .jpeg, .gif, or .png.";
			}
		}
	}
	if(count($errors) > 0)
	{
		echo "<h1>The following errors occured with your slideshow.</h1>";
		echo "<div class='error'>";
		foreach($errors AS $error)
		{
			echo $error . "<br />";
		}
		echo "</div>";
		echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
		//we use javascript to go back rather than reloading the page 
		// so the user doesn't have to type in all that info again.
	}
	else
	{
		//upload to database
		$sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
			  VALUES ('$clientusername','$folder', '$active', '$file')";

		$query = mysql_query($sql) or die(mysql_error());
		echo "</ br><div class=bodyText>Slideshow folders prepared for $clientusername.<br />
			   FTP lightroom slideshow to $folder folder.</div>";
	}
}

do_html_footer();
?> 

Link to comment
Share on other sites

So I've made some minor changes and still can't seem to this to work.  I was getting an undefined variable for $file.  The error stopped, but the data isn't being passed.  i.e. the image still isn't being uploaded and the db isn't populating the file name.

 

Can anyone help?

 

slideshow_new.php

<?php
//Function imported from another file
function protect($string)//function to prevent SQL injection
{
  $string = mysql_real_escape_string($string);
  return $string;
}

	$clientusername = protect($_POST['username']);
	$folder = protect($_POST['foldername']);
	$active = protect($_POST['active']);

	$errors = array();

	if(!$user || !$folder || !$active)
	{
		$errors[] = "You did not fill out the required fields.";
	}

	//Ensure client folder doesn't exist.  Creates client folder
	$sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
			$query = mysql_query($sql) or die(mysql_error());

	if(mysql_num_rows($query) > 0) 
	{
		$errors[] = "Folder name is already in use, please try another";
	}
	else
	{
		mkdir("../Clients/$folder", 0700);
	}

	//Function uploads file
	function upload()
	{
		//Collect all info into variables
		$file = $_FILES['file']['name'];
		$type = $_FILES['file']['type'];
		$temp = $_FILES["file"]["tmp_name"];
		$imageSize = getimagesize($temp);

		//Check file size
		if($imageSize > 1000000)
		{
			$errors[] = "File size must be less than 1MB.";
		}
			if (file_exists("../imagesClients/" . $file))
			{
				$errors[] = "$file image already exists.";
		  	}
		//Check file type
		if (preg_match('/image\/(jpg|jpeg|gif|png)/', $imageSize['mime'])) 
		{
			if (file_exists("../imagesClients/" . $file))
			{
				$errors[] = "$file image already exists.";
		  	}

		//If everything is good then let's upload
		move_uploaded_file($temp, "../imagesClients/" . $file);
		}
		else
		{
			$errors[] = "File type must be .jpg, .jpeg, .gif, or .png.";
		}
	}
	if(count($errors) > 0)
	{
		echo "<h1>The following errors occured with your slideshow.</h1>";
		echo "<div class='error'>";
		foreach($errors AS $error)
		{
			echo $error . "<br />";
		}
		echo "</div>";
		echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
		//we use javascript to go back rather than reloading the page 
		// so the user doesn't have to type in all that info again.
	}
	else
	{
		//upload to database
		$sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
			  VALUES ('$clientusername','$folder', '$active', '$file')";

		$query = mysql_query($sql) or die(mysql_error());
		echo "</ br><div class=bodyText>Slideshow folders prepared for $clientusername.<br />
			   FTP lightroom slideshow to $folder folder.</div>";
	}
}

do_html_footer();
?> 

Link to comment
Share on other sites

Heres a few points for you..

In your upload function you have:

if (file_exists("../imagesClients/" . $file))
{
$errors[] = "$file image already exists.";
}
move_uploaded_file(.....);

Having it set up this way will still attempt to upload the file even it if exists.. you need more if/else statements..

 

Below is a little snippet from an image uploading class that I have written.. it may help you shed some light..

Notably the if/else sections for copy() and move_uploaded_file()

 

<?php
if (file_exists($path) || @mkdir($path)) {
				if (@move_uploaded_file($file['tmp_name'],$path.$file['name'])) {
				} else { 
					if (@copy($file['tmp_name'],$path.$file['name'])) {
					} else {
						$this->alerts[] = 'Upload has failed. Please check the permissions and try again.';
					}
				}
			} else {
				$this->alerts[] = "Unabled to create Directory. Please try again later.";
			}
?>

Link to comment
Share on other sites

You bring up a good point.  I went back and changed a significant portion of the script based on comments provided by ignace on another upload forum.  As of now I'm able to create a folder, upload an image to another folder, and populate the db.  But I am running into problems like you mentioned above.  I'll take another look this evening and see what I can do.  Thanks for the suggestion.

 

Here's my new script:

<?php
session_start();
ini_set ('display_errors', 1);
error_reporting(E_ALL);
require_once('site_fns.php');
include("connect.php");
do_html_header('New Client');
do_menu_main2('');
check_login();
do_mainadmin_menu('');

if(!$_POST['submit']) // 'submit' hasn't been clicked so output html.
{
	//set up form options to select a username	
	$sql = mysql_query("SELECT DISTINCT username FROM Client");
		while($row = mysql_fetch_assoc($sql))
		{
		   $dd .= "<option value='{$row['username']}'>{$row['username']}</option>";
		} 
?>
		<form enctype="multipart/form-data" action="slideshow_new2.php" method="post">
			<fieldset>
				<legend>Setup Slideshow</legend>
				<ol>
					<li>Select: <select name="username" style="width: 222px;">
						<? echo $dd; ?></select></li>
					<li>Folder Name: <input type="text" name="foldername" /></li>
					<li><input type="hidden" name="MAX_FILE_SIZE" valude="10000000" />
						Client Image: <input type="file" name="file" id="file"/></li>
					<li>Active Image: <select name="active">
						<option value="yes" selected="selected">Yes</option>
						<option value="no">No</option></select></li>
					<input type="submit" name="submit" value="Add Slideshow" />
				</ol>
			</fieldset>
		</form>
<?php
}
else
{
	$clientusername = protect($_POST['username']);
	$folder = protect($_POST['foldername']);
	$active = protect($_POST['active']);
	$f = $_FILES['file'];
	$filename = $f['name'];

	$errors = array();
    //make sure form is complete
	if(!$user || !$folder || !$active || !$f)
	{
		$errors[] = "You did not fill out the required fields.";
	}

	//Ensure client folder doesn't exist.  Creates client folder
	$sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
			$query = mysql_query($sql) or die(mysql_error());

	if(mysql_num_rows($query) > 0) 
	{
		$errors[] = "Folder name is already in use, please try another";
	} else {
		mkdir("../Clients/$folder", 0700);
	}

	//Function uploads file
	function isValidImage($imagePath) {
		$imageSize = getimagesize($imagePath);
		return preg_match('/image\/(jpg|jpeg|gif|png)/', $imageSize['mime']) && filesize($imagePath) > 0;
	}

	function formatBytes($bytes) {
		$abbr = array('KB', 'MB', 'GB', 'TB');
		while ($bytes > 1024 && next($abbr)) {
			$bytes /= 1024;
		}
		return $bytes . ' ' . current($abbr);
	}

	$f = $_FILES['file'];
	if (isValidImage($f['tmp_name'])) 
	{
		if (UPLOAD_ERR_OK !== $f['error']) 
		{
			$errors[] =  "Return Code: " . $f['error'] . "<br />";
		} else {
			//echo 'Upload: ' . $f['name'] . '<br />';
			//echo 'Type: ' . $f['type'] . '<br />';
			//echo 'Size: ' . formatBytes($f['size']) . '<br />';
			//echo 'Temp file: ' . $f['tmp_name'] . '<br />';

			$filePath = implode(DIRECTORY_SEPARATOR, array('..','imagesClients', $f['name']));
			if (file_exists($filePath)) 
			{
				$errors[] = $f['name'] . " already exists. <br />";
			} else {
				if (move_uploaded_file($f['tmp_name'], $filePath)) 
				{
					echo "<div class=bodyText>" . $f['name'] . " was stored in: " . $filePath . "<br /></div>";
				}
			}
		}
	}

	if(count($errors) > 0)
	{
		echo "<h1>The following errors occured with your slideshow.</h1>";
		echo "<div class='error'>";
		foreach($errors AS $error)
		{
			echo $error . "<br />";
		}
		echo "</div>";
		echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
		//Javascript to go back rather than reloading the page 
	} else {
		//upload to database
		$sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
			  VALUES ('$clientusername','$folder', '$active', '$filename')";

		$query = mysql_query($sql) or die(mysql_error());
		echo "<div class=bodyText>Slideshow folders prepared for $clientusername.<br /><br />
			   Remember to FTP slideshow to $folder folder.</div>";
	}
}

do_html_footer();
?> 

 

Link to comment
Share on other sites

Making significant progress!!  All my information is being stored, folders are being made and images are uploading as long as the form is filled out without any mistakes.  However, my errors stopped working.  My intentions are to seek out any problems from the form, file types, and directories.  It there's an issue the errors should be reported and no data/files are collected.  However, when fields are left blank partial data is uploaded without displaying any errors.  Help Please!!

 

<?php
if(!$_POST['submit']) // 'submit' hasn't been clicked so output html.
{
	//set up form options to select a username	
	$sql = mysql_query("SELECT DISTINCT username FROM Client");
		while($row = mysql_fetch_assoc($sql))
		{
		   $dd .= "<option value='{$row['username']}'>{$row['username']}</option>";
		} 
?>
		<form enctype="multipart/form-data" action="slideshow_new.php" method="post">
			<fieldset>
				<legend>Setup Slideshow</legend>
				<ol>
					<li>Select: <select name="username" style="width: 222px;">
						<? echo $dd; ?></select></li>
					<li>Folder Name: <input type="text" name="foldername" /></li>
					<li><input type="hidden" name="MAX_FILE_SIZE" valude="10000000" />
						Client Image: <input type="file" name="file" id="file"/></li>
					<li>Active Image: <select name="active">
						<option value="yes" selected="selected">Yes</option>
						<option value="no">No</option></select></li>
					<input type="submit" name="submit" value="Add Slideshow" />
				</ol>
			</fieldset>
		</form>
<?php
}
else
{
	$clientusername = protect($_POST['username']);
	$folder = protect($_POST['foldername']);
	$active = protect($_POST['active']);
	$f = $_FILES['file'];
	$folderDir = '/"../Clients/$folder"';//each folder will hold slideshow for client
	$filename = $f['name'];//each client image will be placed in /clientsImages directory
	$filePath = implode(DIRECTORY_SEPARATOR, array('..','imagesClients', $f['name']));//path to clientsImages

	$errors = array();

	function check_errors()
	{		
		//make sure form is complete
		if(!$user || !$folder || !$active || !$f)
		{
			$errors[] = "You did not fill out the required fields.";
		}

		//Check if foldername exists in DB
		$sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
		$query = mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($query) > 0)
		{
			$errors[] = "Folder name is already in use, please try another.";
		} 

		//Check if directory exists
		if(file_exists($folderDir))
		{
			$errors[] = "Folder already exists.";
		}

		//Check format & size
		function isValidImage($imagePath)
		{
			$imageSize = getimagesize($imagePath);
			return preg_match('/image\/(jpg|jpeg|gif|png)/', $imageSize['mime']) && filesize($imagePath) > 0;
			if($f['type']!='/(jpg|jpeg|gif|png)/')
			{
				$errors[] = "File must be in .jpg, .jpeg, .gif or .png format.";
			}
		}

		function formatBytes($bytes)
		{
			$abbr = array('KB', 'MB', 'GB', 'TB');
			while ($bytes > 1024 && next($abbr))
			{
				$bytes /= 1024;
			}
			return $bytes . ' ' . current($abbr);
		}

		//Check if file already exists
		if (isValidImage($f['tmp_name']))
		{
			if (UPLOAD_ERR_OK !== $f['error'])
			{
				$errors[] =  "Return Code: " . $f['error'] . "<br />";
			}
		}
		if (file_exists($filePath))
		{
			$errors[] = $f['name'] . " already exists. <br />";
		} 
	}

	if(count($errors) > 0)
	{
		echo "<h1>The following errors occured with your slideshow.</h1>";
		echo "<div class='error'>";
		foreach($errors AS $error)
		{
			echo $error . "<br />";
		}
		echo "</div>";
		//Javascript to go back rather than reloading the page 
		echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
	} 
	else 
	{
		//make the directory
		mkdir("../Clients/$folder", 0700);

		//move image to imagesClients
		move_uploaded_file($f['tmp_name'], $filePath);
		echo "<div class=bodyText>" . $f['name'] . " was stored in: <br />" . $filePath . "<br /></div>";

		//upload to database
		$sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
			  VALUES ('$clientusername','$folder', '$active', '$filename')";

		$query = mysql_query($sql) or die(mysql_error());
		echo "<div class=bodyText>Slideshow folders prepared for $clientusername.<br /><br />
			   Don't forget to FTP the slideshow to $folder folder.</div>";
		//echo 'Upload: ' . $f['name'] . '<br />';
		//echo 'Type: ' . $f['type'] . '<br />';
		//echo 'Size: ' . formatBytes($f['size']) . '<br />';
		//echo 'Temp file: ' . $f['tmp_name'] . '<br />';
	}
}		

do_html_footer();

?> 

Link to comment
Share on other sites

Ive made a few modifications to your script..

 

Your check_errors function was wrapping all the other functions but it was never called so I removed it as you will see..

 

<?php

// Varaible declarations //
$errors = array();

// Function declarations//

function isValidImage($imagePath)
{
global $errors;
$imageSize = getimagesize($imagePath);
return preg_match('/image\/(jpg|jpeg|gif|png)/', $imageSize['mime']) && filesize($imagePath) > 0;
if($f['type']!='/(jpg|jpeg|gif|png)/') {
	$errors[] = "File must be in .jpg, .jpeg, .gif or .png format.";
}
}
            
function formatBytes($bytes){
$abbr = array('KB', 'MB', 'GB', 'TB');
while ($bytes > 1024 && next($abbr)) {
	$bytes /= 1024;
}
return $bytes . ' ' . current($abbr);
}
         
         
if(!$_POST['submit']) // 'submit' hasn't been clicked so output html.
   {
      //set up form options to select a username   
      $sql = mysql_query("SELECT DISTINCT username FROM Client");
         while($row = mysql_fetch_assoc($sql))
         {
            $dd .= "<option value='{$row['username']}'>{$row['username']}</option>";
         } 
?>
         <form enctype="multipart/form-data" action="slideshow_new.php" method="post">
            <fieldset>
               <legend>Setup Slideshow</legend>
               <ol>
                  <li>Select: <select name="username" style="width: 222px;">
                     <? echo $dd; ?></select></li>
                  <li>Folder Name: <input type="text" name="foldername" /></li>
                  <li><input type="hidden" name="MAX_FILE_SIZE" valude="10000000" />
                     Client Image: <input type="file" name="file" id="file"/></li>
                  <li>Active Image: <select name="active">
                     <option value="yes" selected="selected">Yes</option>
                     <option value="no">No</option></select></li>
                  <input type="submit" name="submit" value="Add Slideshow" />
               </ol>
            </fieldset>
         </form>
<?php
   }
   else
   {
      $clientusername = protect($_POST['username']);
      $folder = protect($_POST['foldername']);
      $active = protect($_POST['active']);
      $f = $_FILES['file'];
      $folderDir = '/"../Clients/$folder"';//each folder will hold slideshow for client
      $filename = $f['name'];//each client image will be placed in /clientsImages directory
      $filePath = implode(DIRECTORY_SEPARATOR, array('..','imagesClients', $f['name']));//path to clientsImages
           
      //make sure form is complete
      if(!$user || !$folder || !$active || !$f)
         {
            $errors[] = "You did not fill out the required fields.";
         }
         
         //Check if foldername exists in DB
         $sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
         $query = mysql_query($sql) or die(mysql_error());
         if(mysql_num_rows($query) > 0)
         {
            $errors[] = "Folder name is already in use, please try another.";
         } 
         
         //Check if directory exists
         if(file_exists($folderDir))
         {
            $errors[] = "Folder already exists.";
         }
         
                  
         //Check if file already exists
         if (isValidImage($f['tmp_name']))
         {
            if (UPLOAD_ERR_OK !== $f['error'])
            {
               $errors[] =  "Return Code: " . $f['error'] . "<br />";
            }
         }
         if (file_exists($filePath))
         {
            $errors[] = $f['name'] . " already exists. <br />";
         } 
      }
         
      if(count($errors) > 0)
      {
         echo "<h1>The following errors occured with your slideshow.</h1>";
         echo "<div class='error'>";
         foreach($errors AS $error)
         {
            echo $error . "<br />";
         }
         echo "</div>";
         //Javascript to go back rather than reloading the page 
         echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
      } 
      else 
      {
         //make the directory
         mkdir("../Clients/$folder", 0700);
         
         //move image to imagesClients
         move_uploaded_file($f['tmp_name'], $filePath);
         echo "<div class=bodyText>" . $f['name'] . " was stored in: <br />" . $filePath . "<br /></div>";
                  
         //upload to database
         $sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
              VALUES ('$clientusername','$folder', '$active', '$filename')";
         
         $query = mysql_query($sql) or die(mysql_error());
         echo "<div class=bodyText>Slideshow folders prepared for $clientusername.<br /><br />
               Don't forget to FTP the slideshow to $folder folder.</div>";
         //echo 'Upload: ' . $f['name'] . '<br />';
         //echo 'Type: ' . $f['type'] . '<br />';
         //echo 'Size: ' . formatBytes($f['size']) . '<br />';
         //echo 'Temp file: ' . $f['tmp_name'] . '<br />';
      }
   }      

   do_html_footer();
   
?>

Link to comment
Share on other sites

That seems to have done the trick.  I really appreciate your help.

 

Before I close out this forum I'm getting the following error when I submit the form without selecting a file:

 

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home/brittao1/public_html/admin/slideshow_new.php on line 20

 

Any suggestions?

Link to comment
Share on other sites

try this instead, ive never used getimagesize.

 

<?php

function isValidImage($image) {
global $errors;
$valid = true;
$valid_ext = array('png','jpeg','gif','jpg');
if (!in_array(end(explode(".", strtolower($image))), $valid_ext)) { 
	$valid = false;			
}
if ($err) {
	$errors[] = "File must be in one of the following formats: - ".join('<br/>- ',$valid_ext);
}
return $valid;
}
}

?>

 

And change this:

if (isValidImage($f['tmp_name']))

to

if (isValidImage($f['name']))

Link to comment
Share on other sites

Oops use this.. I think I need some sleep..

 

function isValidImage($image) {
   global $errors;
   $valid = true;
   $valid_ext = array('png','jpeg','gif','jpg');
   if (!in_array(end(explode(".", strtolower($image))), $valid_ext)) { 
      $valid = false;         
   }
   if (!$valid) {
      $errors[] = "File must be in one of the following formats: - ".join('<br/>- ',$valid_ext);
   }
   return $valid;
}

Link to comment
Share on other sites

All the errors after submit are fixed.  Thanks a bunch.  However, now I'm getting the following error:

 

Fatal error: Call to undefined function isvalidimage() in /home/brittao1/public_html/admin/slideshow_new9.php on line 111

 

referring to:

if (isValidImage($f['name'])){
    if (UPLOAD_ERR_OK !== $f['error']){
        $errors[] =  "Return Code: " . $f['error'] . "<br />";
    }
}

 

Here's everything so far:

 

			<?php
				if(!$_POST['submit']){ // 'submit' hasn't been clicked so output html.
						//set up form options to select a username	
						$sql = mysql_query("SELECT DISTINCT username FROM Client");
							while($row = mysql_fetch_assoc($sql)){
							 $dd .= "<option value='{$row['username']}'>{$row['username']}</option>";
							} 
			?>

			<form enctype="multipart/form-data" action="slideshow_new9.php" method="post">
				<fieldset>
					<legend>Setup Slideshow</legend>
					<ol>
						<li>Select: <select name="username" style="width: 222px;">
							<? echo $dd; ?></select></li>
						<li>Folder Name: <input type="text" name="foldername" /></li>
						<li><input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
							Client Image: <input type="file" name="file" id="file"/></li>
						<li>Active Image: <select name="active">
							<option value="yes" selected="selected">Yes</option>
							<option value="no">No</option></select></li>
						<input type="submit" name="submit" value="Add Slideshow" />
					</ol>
				</fieldset>
			</form>
			<?php	
				}
			   else
			   {
				// Varaible declarations //
				$clientusername = protect($_POST['username']);
				$folder = protect($_POST['foldername']);
				$active = protect($_POST['active']);
				$f = $_FILES['file'];
				$folderDir = '/"../Clients/$folder"';//each folder will hold slideshow for client
				$filename = $f['name'];//each client image will be placed in /clientsImages directory
				$filePath = implode(DIRECTORY_SEPARATOR, array('..','imagesClients', $f['name']));//path to clientsImages
				$errors = array();

				// Function declarations//

				function isValidImage($image) {
				   global $errors;
				   $valid = true;
				   $valid_ext = array('png','jpeg','gif','jpg');
				   if (!in_array(end(explode(".", strtolower($image))), $valid_ext)) { 
					  $valid = false;         
				   }
				   if ($err) {
					  $errors[] = "File must be in one of the following formats: - ".join('<br/>- ',$valid_ext);
				   }
				   return $valid;
				   }
				}

				function formatBytes($bytes){
				   $abbr = array('KB', 'MB', 'GB', 'TB');
				   while ($bytes > 1024 && next($abbr)) {
					  $bytes /= 1024;
				   }
				   return $bytes . ' ' . current($abbr);
				}						   
					   
				  //make sure form is complete
				  if(!$user || !$folder || !$active || !$f){
						$errors[] = "You did not fill out the required fields.";
					 }

					 //Check if foldername exists in DB
					 $sql = "SELECT * FROM Slideshow WHERE (foldername) = ('$folder')";
					 $query = mysql_query($sql) or die(mysql_error());
					 if(mysql_num_rows($query) > 0){
						$errors[] = "Folder name is already in use, please try another.";
					 } 

					 //Check if directory exists
					 if(file_exists($folderDir)){
						$errors[] = "Folder already exists.";
					 }

					 //Check if file already exists
					 if (isValidImage($f['name'])){
						if (UPLOAD_ERR_OK !== $f['error']){
						   $errors[] =  "Return Code: " . $f['error'] . "<br />";
						}
					 }
					 if (file_exists($filePath)){
						$errors[] = $f['name'] . " already exists. <br />";
					 } 

				  if(count($errors) > 0){
					 echo "<h1>The following errors occured with your slideshow.</h1>";
					 echo "<div class='error'>";
					 foreach($errors AS $error){
						echo $error . "<br />";
					 }
					 echo "</div>";
					 //Javascript to go back rather than reloading the page 
					 echo "<a href=\"javascript:history.go(-1)\">Try again</a>";
				} 
				else 
				{
					 //make the directory
					 mkdir("../Clients/$folder", 0700);

					 //move image to imagesClients
					 move_uploaded_file($f['tmp_name'], $filePath);
					 echo "<div class=bodyText>" . $f['name'] . " was stored in: <br />" . $filePath . "<br /></div>";
							  
					 //upload to database
					 $sql = "INSERT INTO Slideshow (username, foldername, active, image_name)
						  VALUES ('$clientusername','$folder', '$active', '$filename')";

					 $query = mysql_query($sql) or die(mysql_error());
					 echo "<div class=bodyText>Slideshow folders prepared for $clientusername.<br /><br />
						   Don't forget to FTP the slideshow to $folder folder.</div>";
					 //echo 'Upload: ' . $f['name'] . '<br />';
					 //echo 'Type: ' . $f['type'] . '<br />';
					 //echo 'Size: ' . formatBytes($f['size']) . '<br />';
					 //echo 'Temp file: ' . $f['tmp_name'] . '<br />';
				}
			?>

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.