不知道如何将现有图像上载功能添加到新表单

时间:2013-03-26 13:10:03

标签: php html forms function variables

我是一位相当新手的网站管理员,正努力为我的用户设置机密分类。 (我基本上正在重建写得不好的分类广告)

我买了一个廉价的分类广告程序,所以我有一些东西可以学习PHP,我已经走了很长一段路,但我还有很长的路要走,因为我不知道如何将现有图像上传的所有内容联系在一起功能,如果有人不介意帮助我,我会非常感激。

请允许我解释一下;

在这个分类广告的早期,访问者必须添加他们的列表(这是一个婊子)然后访问者必须去寻找他们可以上传他们的图像的地方。毋庸置疑,这并没有削减它。

快速前进到现在;现在我在页面上有一个带有几个文本输入的表单(即; text,select,textarea和一个复选框)以及在此表单上我有4个图像上传按钮,如下所示。

<form id="generalform" class="container" method="POST"  action="addlistingprocess.php" autocomplete="on" enctype="multipart/form-data" >

        <div class='field'>
        <label for='TypeID'>I'm Selling A:</label>
        <select name='TypeID'>
        <option value='42'>Motorcycle</option>
        <option value='43'>New or Used Part</option>
        </select>
        </div>

        <div class="field">
        <label for="Year">* Year:</label>
        <input type="number" class="input" name="Year" maxlength="5" min="1963" max="2013" value="2013">
        </div>

        <div class="field">
        <label for="fatherID">* Make:</label>
        <select name='fatherID'>
        <option>-- Select Manufacturer --</option>
        <option value='10'>Honda</option>
        <option value='35'>Husqvarna</option>
        <option value='36'>Kawasaki</option>
        <option value='37'>KTM</option>
        <option value='38'>Suzuki</option>
        <option value='39'>Yamaha</option>
        <option value='40'>Other</option>
        <option value='46'>It's Vintage or Evo</option>
        </select>
        </div>

        <div class="field">
        <label for="Model">* Model:</label>
        <input type="text" class="input" name="Model" maxlength="20" value="<?php if(isset($_POST['Model'])) echo $_POST['Model']; ?>" placeholder="(i.e. CRF450F, KX 450F...)">
        </div>

        <div class="field">
        <label for="Description">* Details About It:</label><br>
        <textarea name="Description" rows="10" cols="60"><?php if(isset($_POST['Description'])) echo $_POST['Description']; ?></textarea>
        </div>

        <div class="field">
        <label for="Vin">VIN:</label>
        <input type="text" class="input" name="VIN" maxlength="30" value="<?php if(isset($_POST['VIN'])) echo $_POST['VIN']; ?>" placeholder="Optional">
        </div>

        <div class="field">
        <label for="Price">* Asking Price ($):</label>
        <input type="number" class="input" name="Price" maxlength="10" min="0" value="<?php if(isset($_POST['Price'])) echo $_POST['Price']; ?>">
        </div>
        <!--
        <h4>Upload Up To 4 Images</h4>

        <div class="field">
        <label for="Image">Image 1</label>
        <input type="file" class="fileuploadinput" name="Image1">
        <label for="Image">Image 2</label>
        <input type="file" class="fileuploadinput" name="Image2">
        <label for="Image">Image 3</label>
        <input type="file" class="fileuploadinput" name="Image3">
        <label for="Image">Image 4</label>
        <input type="file" class="fileuploadinput" name="Image4">
        </div>
                -->
        <br>
        <hr style="width:60%">
        <br>

        <h3>Where It's Located:</h3>
        <div class="field">
        <label for="Address">Address:</label>
        <input type="text" class="input" id="Address" name="Address" maxlength="40" placeholder="Optional" />
        <p class="hint">40 Characters Maximum</p>   
        </div>

        <div class="field">
        <label for="City">* City:</label>
        <input type="text" class="input" id="City" name="City" maxlength="20" />
        <p class="hint">20 Characters Maximum</p>   
        </div>

        <div class="field">
        <label for="State">* State / Province:</label>
        <input type="text" class="input" id="State" name="State" maxlength="20" />
        <p class="hint">20 Characters Maximum</p>   
        </div>

        <div class="field">
        <label for="ZIP">* ZIP:</label>
        <input type="text" class="input" id="ZIP" name="ZIP" maxlength="20" />
        <p class="hint">20 Characters Maximum</p>   
        </div>

        <div class="field">
        <label for="Country">* Country:</label>
        <select name="Country">
<option selected="selected" value="United States">United States</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="Zimbabwe">Zimbabwe</option>
</select>
</div>      
        <input type="checkbox" name="Inform" value="1" checked/> Keep Me Dialed In With Site Updates & Specials<br><br>

        <input type="submit" name="submit" id="submit" class="button" value="Submit My Ad"/>
        </form>

现在这里是踢球者,(抱歉创造这么长的帖子)

这个分类广告已经有了一个现有的图片上传功能,我想保留它,因为它处理调整大小和显示图像,如下所示: http://classifieds.your-adrenaline-fix.com/detail.php?fatherID=10&TypeID=42&ListingID=7

我想将此功能与上面的表单联系起来,但是,这个程序需要很多参数,我已经尝试了几周才能理解,但仍然没有。 (但是为了理解它,我已经清除了代码中的sh ^ $)

我将以所有适用的文件作为代码关闭此帖子,如果有人可以帮助我,我将非常感激,并且我提前感谢你们。

这是images.php。 (这是访问者必须导航到创建广告之后的位置,但也是访问者可以添加或删除与其列表相关联的照片的位置,因此我需要保留此内容)

<?php
    include('header.php');  

    if(!$superUser){
        if(!$login){            
            echo "<h2>Please Login to Manage Photos</h2>";
            include('z-login-form.php');
            include('no-ad-footer.php');
            die();
            }
        }

    if($_POST['POrder'] != "") {    
        if (($_FILES['file']['type'] != 'image/jpeg') && ($_FILES['file']['type'] != 'image/jpg') && ($_FILES['file']['type'] != 'image/pjpeg')) {
            echo "<script>alert('Images Must be in jpg format and under 2 Mb');\n";
            echo sprintf("window.location='images.php?TypeID=%s&ListingID=%s'", $_POST['TypeID'], $_POST['ListingID']);
            echo "</script>";
            include('no-ad-footer.php');
            die();
        }

        chdir('admin/photos'); 
        require_once('upload.php');
        chdir('../../');

    if ($up->ValidateUpload()) { 
            $node = new sqlNode();
            $node->table = "photos";
            $node->push("int","POrder",$_POST['POrder']);
            $node->push("text","Location",$new_name);
            $node->push("int","TypeID",$_POST['TypeID']);
            $node->push("int","ListingID",$_POST['ListingID']);

            if(($result = $mysql->insert($node)) === false)
                die('Unable to push POrder, Location, TypeID and ListingID into table photos line 38');         
        } else {
            echo "<font color='red'>Unable to upload image</font>";
            }
    }   

    if(($_REQUEST['TypeID'] != "") && ($_REQUEST['ListingID'] != "")) {     
        if(!$superUser){
            echo "<a href='memberindex.php'>Return to My Listings</a>";
        }       

        $sql = sprintf("SELECT * FROM `types` WHERE ID = %s", intval($_REQUEST['TypeID']));
        $result = $mysql->exSql($sql) or die('Unable to Retrieve Type ID from table types');
        if(mysql_num_rows($result)<1){
            die('Less Than One Result Returned from table types. Line51 images.php');
        }

        if(!$superUser){
            $sql = sprintf("SELECT * FROM `tt_%s` WHERE MemberID = %s AND ID = %s", abs(intval($_REQUEST['TypeID'])), intval($_SESSION['memberID']), intval($_REQUEST['ListingID']));
            $result = $mysql->exSql($sql) or die('Unable to select data from table tt__');
            if(mysql_num_rows($result)<1){
                die("<script>window.location='logout.php';</script>");
            }
        }

        $node = new sqlNode();
        $node->table = "photos";
        $node->select = "*";
        $node->where = "WHERE TypeID = ".intval($_REQUEST['TypeID'])." AND ListingID = ".intval($_REQUEST['ListingID']);
        $node->orderby = "ORDER BY `POrder` ASC";

        if(($result = $mysql->select($node)) === false )
            die('Unable to Retrieve data from table photos');

        $num_of_photos = mysql_num_rows($result);

        echo "<h2>Upload or Delete Photos</h2>";

        $showform = true;

        if($showform) {     
            echo "<form action='images.php' enctype='multipart/form-data' method='POST'>";
            echo "<input type='hidden' name='TypeID' value='".$_REQUEST['TypeID']."'>";
            echo "<input type='hidden' name='ListingID' value='".$_REQUEST['ListingID']."'>";
            echo "<input type='hidden' name='Upload' value='true'>";
            echo "<input type='hidden' name='POrder' value='".($num_of_photos+1)."'>";

            echo "<table border=0>";
            echo "<tr>";
            echo "<td>Image 1</td><td><input type='file' name='file' accept='image/*' id='file'></td>"; 
            echo "</tr>";
            echo "<tr>";
            echo "<td>&nbsp;</td><td><input type='submit' value='Upload File(s)'></td>";
            echo "</tr>";
            echo "</table>";
            echo "</form>";
        } else {
            echo "<font color='red'>Max Number of Photos Reached</font>";
        }

        if($num_of_photos >=1) {
        //Print photo table
        echo "<br>";
        echo "<center><strong>$num_of_photos Photo(s) Associated With This Listing</strong></center>";      
        echo "<table class='subHeader' align='center'>";
        echo "<tr>";
        echo "<td>Photo</td><td>Order&nbsp;|&nbsp;</td><td>Action</td>";
        echo "</tr>";

        //For each photo
        while($photo = mysql_fetch_assoc($result)){
            echo "<tr>";
            echo "<td valign=top align=center>";
            echo "<a target='_blank' href='admin/photos/uploads/".$photo['Location']."' title='Click To View Full Size Photo'><img src='admin/photos/uploads/small_thumbs/tn_".$photo['Location']."' border=0></a>";
            echo "</td>";
            echo "<td>".$photo['POrder']."</td>";
            echo "<td>";
            echo "<a href='deleteimage.php?TypeID=".$photo['TypeID']."&ListingID=".$photo['ListingID']."&PhotoID=".$photo['ID']."'>Delete</a>";
            echo "</td>";
            echo "</tr>";
        }

        echo "</table>";
    }
    }
            echo "</td>";
            echo "</tr>";
            echo "</table>";  


    include('no-ad-footer.php');

?>

这是upload.php

<?php
require_once("UploadFile.class.php");   
$up = new UploadImage($file, 2097152, 3000, 3000,'file',"uploads", $rename_file=true);

if(true) {                                         
    $new_name = $up->CopyFile();
    $imagefolder='.';
    $thumbsfolder='.';
    echo "Creating thumbnail image...<br>";

    function createthumb($name,$filename,$new_w,$new_h) {
        $system=explode(".",$name);
        if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
        if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);
        if ($old_x > $old_y) {
            $thumb_w=$new_w;
            $thumb_h=$old_y*($new_h/$old_x);
        }
        if ($old_x < $old_y) {
            $thumb_w=$old_x*($new_w/$old_y);
            $thumb_h=$new_h;
        }
        if ($old_x == $old_y){
            $thumb_w=$new_w;
            $thumb_h=$new_h;
        }
        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
        if (preg_match("/png/",$system[1])){
            imagepng($dst_img,$filename); 
        } else {
            imagejpeg($dst_img,$filename); 
        }

        imagedestroy($dst_img); 
        if(!imagedestroy($src_img)){
            $return = @unlink($filename);
            $return2 = @unlink($name);
            // Return FALSE if not found
            var_dump($return);
            var_dump($return2);
?>
            <script>alert("Unable to resize photos. Please check file/folder permission in admin/photos.");window.location ="index.php";</script>
<?php
        }

    }
    createthumb("uploads/" . $new_name,"uploads/thumbs/tn_" . $new_name,350,350);
    createthumb("uploads/" . $new_name,"uploads/small_thumbs/tn_" . $new_name,150,150);

    function ditchtn($arr,$thumbname){
        foreach ($arr as $item) {
            if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
        }
        return $tmparr;
    }

    function directory($dir,$filters){
        $handle=opendir($dir);
        $files=array();
        if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
        if ($filters != "all"){
            $filters=explode(",",$filters);
            while (($file = readdir($handle))!==false){
                for ($f=0;$f<sizeof($filters);$f++):
                    $system=explode(".",$file);
                    if ($system[1] == $filters[$f]){$files[] = $file;}
                endfor;
            }
        }
        closedir($handle);
        return $files;
    }
}
?>

这是UpLoadFileclass.php

<?php 
function  RandomFile($pass_len=12) {                   
            $allchar = "abcdefghijklmnopqrstuvwxyz" ; 
            $str = "" ; 
            mt_srand (( double) microtime() * 1000000 ); 
              for ($i=0; $i<$pass_len; $i++) 
                  $str .= substr( $allchar, mt_rand (0,25), 1 ) ; 
                  return $str ;
                  }    

      class UploadImage {         
                   var $image; 
                   var $imagesize; 
                   var $max_file_size; 
                   var $max_file_height; 
                   var $max_file_width; 
                   var $allowed; 
                   var $rename_file; 
                   var $path; 

       function UploadImage($image, $max_file_size, $max_file_height, $max_file_width, $field_name, $path, $rename_file=true) {                     
             $this->image=$image; 
             $this->max_file_size=$max_file_size; 
             $this->max_file_height=$max_file_height; 
             $this->max_file_width=$max_file_width; 
             $this->allowed = array( ".jpg"=>"2", ".jpeg"=>"2"); 
             $this->field_name = $field_name; 
             $this->path = $path; 
             $this->rename_file = $rename_file; 
        } 

        function ValidateUpload() {            
             if ($this->max_file_size <  filesize($this->image)) {                   
                  print("<span style=\"color:red;\">ERROR: Your File: ".$_FILES[$this->field_name]["name"]." is ".filesize($this->image)." KB, The max file size allowed is ".$this->max_file_size."</span>"); 
                    return false;                  
             }              
                $this->imagesize=getimagesize($this->image); 

                if ($this->max_file_width < $this->imagesize[0]) { 
                    print("<span style=\"color:red;\">ERROR:<br />Your File: ".$_FILES[$this->field_name]["name"]." is ".$this->imagesize[0]." pixels wide, the max file width allowed is ".$this->max_file_width."</span>");                     
                    return false;                        
                } 

                 if ($this->max_file_height < $this->imagesize[1]) {                        
                     print("<span style=\"color:red;\">ERROR:<br />Your File: ".$_FILES[$this->field_name]["name"]." is ".$this->imagesize[1]." pixels high, the max file height allowed is ".$this->max_file_height."<span>");                      
                      return false;                        
                }               
                return true; 
        } 

        function PrintForm($max_file_size) {           
              global $PHP_SELF;     
              print("<form action=\"$PHP_SELF\" method=\"POST\" enctype=\"multipart/form-data\"></br>\n"); 
              print("<input type=file name=$this->field_name><br>\n");     
              print("<input type=hidden name=max_file_size value=".$max_file_size."><br>\n"); 
              print("<input type=submit name=submit><br>\n"); 
              print("</form>");                       
            }

        function CopyFile() {
            if($this->rename_file) {
            global $name, $ext;     

      switch($_FILES[$this->field_name]["type"]) { 

       case 'image/gif'; 
       $ext="gif"; 
       break; 

        case 'image/jpeg'; 
       $ext="jpg"; 
       break; 

        case 'image/pjpeg'; 
       $ext="jpg"; 
       break; 

        case 'image/png'; 
       $ext="png"; 
       break; 

        case 'application/x-shockwave-flash'; 
       $ext="swf"; 
       break; 

       case 'image/psd'; 
       $ext="psd"; 
       break; 

       case 'image/bmp'; 
       $ext="bmp"; 
       break; 
     } 

     $name=RandomFile();

         if(!copy($_FILES[$this->field_name]["tmp_name"],$this->path."/".$name.".".$ext)){
               print("<strong>There has been an error while uploading Filename:".$_FILES[$this->field_name]["name"] ."</strong>");                  
           } else {                
               print("Filename: ".$_FILES[$this->field_name]["name"] ." has been uploaded"); 
               //return  new file name
               return $name.".".$ext;                
           } 
           } else { 
           if(!file_exists($this->path."/".$_FILES[$this->field_name]["name"])) {                
                     if(!copy($_FILES[$this->field_name]["tmp_name"],$this->path."/". $_FILES[$this->field_name]["name"])) {
                        print("There has been an error uploading".$_FILES[$this->field_name]["name"]."please try again");                  
                        } else {
                            print("Filename:".$_FILES[$this->field_name]["name"]." has been uploaded");                
                   } 
                   } else { 
                   print("ERROR: A file by this name already exists"); 
                  } 
                 } 
                } 
               } 

?>

再一次,如果有人能帮助我理解所有这一切,我会非常感激,我再次感谢你们(提前)

1 个答案:

答案 0 :(得分:0)

您是否尝试将包含添加到表单所在的页面?

include_once("upload.php");
include_once("UpLoadFileclass.php");