Jump to content

Storing an image path and tags problem


SeasonEnds

Recommended Posts

So, I am your typical newbie to php. I am usually doing more design stuff, but now I am diving into PHP. The script in question is made for a user to upload an image to a respective directory. Also the path, category, and 2 tags representing the image will be uploaded to a mySQL database.

 

My problem is that I have pieced together some freeware scripts that I have found in order to accomplish my ultimate goal. I need the form to be processed on the same page, because I don't want to redirect users. I also need to upload the respective information to the database. Right now, when you upload an image it only seems to be processing a part of the form, and does not upload any of the information at all. Can someone spot the many things that I am doing wrong and provide me with some direction?

 

I have two files in question

 

image_upload.php (Sorry about all of the code)

<?php
require_once 'upload_config.php';

function VerifyForm(&$values, &$errors)
{

// Do all necessary form verification
    if($clear_folder_before_upload){
        $mydirectory = myUploadDir();
        EmptyDir($mydirectory);
    }

    $uploaded_file_counter=0;
    $UploadLimit = $_POST['counter'];
    
    for($i=0;$i<=$UploadLimit;$i++){
        $file_tag='filename'.$i;
        $filename=$_FILES[$file_tag]['name'];

            if($filename!=null) {
            $rand=time();
            $str="$rand$filename";

            // set folder name in here.
            $filedir= myUploadDir();

            //change the string format.
            $string= $filedir.$str;
            $patterns[0] = "/ /";
            $patterns[1] = "/ /";
            $patterns[1] = "/ /";
            $replacements[1] = "_";
            $dirname=strtolower(preg_replace($patterns, $replacements, $string));
            //end of changing string format

            //checking the permitted file types
                            if($check_file_extentions)    {
                            $allowedExtensions = allowedfiles();
                                foreach ($_FILES as $file) {
                                                    if ($file['tmp_name'] > '') {
                                                      if (!in_array(end(explode(".",
                                                            strtolower($file['name']))),
                                                            $allowedExtensions)) {
                                                            $fileUploadPermission=0;
                                                      }
                                                      else
                                                      {
                                                          $fileUploadPermission=1;
                                                      }
                                                    }
                                                  }
                            }
                            else{
                                $fileUploadPermission=1;
                            }
            //end of checking the permitted file types

                if($fileUploadPermission){
                    if(move_uploaded_file($_FILES[$file_tag]['tmp_name'],$dirname)) {
                            echo "<img src='$dirname'>";
                            $uploaded_file_counter+=1;
                        }
                }
                
            }

    }

if($uploaded_file_counter==0){
    	echo "<br /> <b style='font-weight:bold;color:red'>Oops! Please select an image file</b>";
}else{
    	echo "<br /> <b>You requested ".$i." image files to upload and ".$uploaded_file_counter." files uploaded sucessfully</b>";
	echo $filename0;
	$contentdiv = 'Upload succesful! Upload Again';
}


}

function DisplayForm($values, $errors)
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JayGilford.com :: Name form example</title>
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js"></script>

    <style type="text/css">
        div#upload_box_wrapper{width:350px;text-align:center; float: left;font-family: verdana; font-size: 12px;padding: 5px 5px 5px 5px; color: black;}
        div#upload_box_wrapper #upload_title_text{text-align:left;width: 100%;display: block;border: 0px solid green; margin-bottom: 5px;}
        div#upload_box_wrapper #FileUploadDiv{display: block;width: 98%;border: 0px solid green; text-align: left;padding: 0px 0px 0px 0px;}
        div#upload_box_wrapper p.add-new-upload-box{float: left;width: 100%;text-align: left;}
        div#upload_box_wrapper a.add-new-upload-link{font-weight: bold;font-size: 13px;padding-right: 3px;text-decoration: none; color: black;}
        div#upload_box_wrapper .upload-button{border: 0px solid lightgreen;height:30px; width:120px;cursor: pointer;background:url(./images/upload_button.png) no-repeat center left; color: white;} 
    </style>
</head>
<body>
<form method='post' action='' enctype='multipart/form-data'>
        <div id="upload_box_wrapper">

                <div id="upload_title_text">Image Upload:</div>
                    <div id="FileUploadDiv" DivValue="0">
                        <input name='filename0' type='file' id='filename'>
                        <select name="img_category">
						<option value="Vacation">Vacation</option>
						<option value="Holiday">Holiday</option>
					</select>
                        <input name="tag1" type="text" id="tag1">
                        <input name="tag2" type="text" id="tag2">
                    </div>
                    <input type="hidden" value="0" id="counter" name="counter">
                    <div align='left'>
                        <input type='submit' name='submit' value='Upload Now!' class="upload-button">
                    </div>
	</div>
</form>

</body>
</html>


<?php
}

function ProcessForm($values)
{

$filename0 = $_POST['filename'];
$filetype = $_POST[''];
$img_category = $_POST['img_category'];
$tag1 = $_POST['tag1'];
$tag2 = $_POST['tag2'];

////////
//figure out the image??
/////

session_start();
$img = $_POST[ 'imglink' ];
$filetypes = array( 'gif','jpg','jpeg','png' );
++$filetypes;
$filetype = strstr( $img, '.' );
if( $filetype != $filetypes )
{ echo 'not supported'; }
session_register( 'Image' );


$Img = $_SESSION[ 'Image' ];
mysql_query( "INSERT into `images` ( `image_id`, `filename`, `filetype`, `url`, `img_category`, `tag1`, `tag2` ) 
		 VALUES ( '', '$filename0', '$filetype', '$Img', '$img_category', '$tag1', '$tag2' )" );
session_destroy();
mysql_close( $DB );

// Replace with actual page or redirect 

}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$formValues = $_POST;
$formErrors = array();

if (!VerifyForm($formValues, $formErrors))
DisplayForm($formValues, $formErrors);
else
ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>

 

and the upload_config.php file

 

<?php
///////////////////////////////////////////////////////////////////////////
//establish mysql connection (I have this set correctly on the original file)
///////////////////////////////////////////////////////////////////////////
$db = mysql_connect('RESPECTIVE', 'MYSQL', IDANDPW');

if (!$db) {
    echo "Unable to establish connection to database server";
    exit;
}

if (!mysql_select_db('chad_images', $db)) {
    echo "Unable to connect to database";
    exit;
}

///////////////////////////////////////////////////////////////////////////
// set your upload directory name in here.
///////////////////////////////////////////////////////////////////////////
function myUploadDir(){
    $myUploadDir = "./upload/";
    return $myUploadDir;
}

///////////////////////////////////////////////////////////////////////////
// if you want to imposs file type rescriction then
// $check_file_extentions=1  or if you want to off this option then
// $check_file_extentions=0
///////////////////////////////////////////////////////////////////////////

$check_file_extentions = 1;

////////////////////////////////////////////////////////////////////////////
// set your allowed type in the array.
// examples: zip, pdf, psd and many more.
//
////////////////////////////////////////////////////////////////////////////
function allowedfiles()
                {
                        $allowed_file_extensions=array("png","jpg","gif","bmp");
                        return $allowed_file_extensions;
                }

///////////////////////////////////////////////////////////////////////////
// if you want to delete all the files in uploaded directory
// $clear_folder_before_upload = 1  or if you want to off this option then
// $clear_folder_before_upload = 0
///////////////////////////////////////////////////////////////////////////

$clear_folder_before_upload = 1;

function EmptyDir($dir) {

    $handle=opendir($dir);
    while (($file = readdir($handle))!==false) {
    @unlink($dir.'/'.$file);
    }
    closedir($handle);
}

?>

 

Any help would be greatly appreciated!

 

Link to comment
https://forums.phpfreaks.com/topic/224017-storing-an-image-path-and-tags-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.