Jump to content

Need help Rename the image while uploading.


Vinayshukla404

Recommended Posts

Hi friends, 

I need help renaming image  while uploading, Its a old college project. in which i am taking admission. user register on site and get a username and password, then login to fill the form. after filling form he goes to next step in which he uploads documents.

the documenst.php file code is this.

 

<?php


error_reporting(0);
session_start();

include 'fileupload.php';   
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Upload Documents</title>
        <link type="text/css" rel="stylesheet" href="css/admform.css"></link>
        <link rel="stylesheet" href="css/indexcss.css">
        <link rel="stylesheet" href="bootstrap/bootstrap.min.css">
         <link rel="stylesheet" href="bootstrap/bootstrap-theme.min.css">
       <script src="bootstrap/jquery.min.js"></script>
        <script src="bootstrap/bootstrap.min.js"></script>
        
        <script type="text/javascript">
            
            function send()
            {
                if(document.getElementById('dec').checked)
                    {
                        window.location='admsnreport.php';
                        return false;
                    }
                    return true;
            }
        </script>
    </head>
    <body style="background-image:url('./images/inbg.jpg');">
        <form id="docup" enctype="multipart/form-data" name="docup" action="documents.php" method="post">
            <div class="container-fluid"> 
				<div class="topnav">
				  <a href="#home">Home</a>
				  <a href="adminlogin.php">Admin</a>
				  <a class="active" href="homepageuser.php">Student</a>
				  <a href="important.php">Important</a>
				  <a href="logout.php">Logout</a>
				</div>
                <div class="row">
                  <div class="col-sm-12">
                        <img src="images/cutm.jpg" width="100%" style="box-shadow: 1px 5px 14px #999999; "></img>
                  </div>
                 </div>    
             </div>
            <div class="container" style="margin-left:100px;">
            <table class="table table-striped">
                            <thead>
                                       <tr>
                                           <th >
                                       <font style=" font-family: Verdana;  font-size:19px;"> Upload Your Documents</font>
                                           </th>
											<th >
                                       <p style=" font-family: Verdana;  font-size:13px;"> File Size should no Exceed 1 MB each.</p>
                                           </th>
                                        </tr>           
                            </thead>
                            <tbody>
                            <tr>
                                 <td>Passport Size Image :</td>
                                 <td><input type="file" id="fpic" name="fpic" ><br>
                                     </td>
                            </tr> 
                           
                             <tr>
                                <td>10th Mark Sheet :</td>
                                <td>
                                     <input type="file" id="ftndoc" name="ftndoc"><br>
                                     
                                 </td>
                             </tr>
                             
							 <tr>
                                 <td>
                                     12th Mark Sheet :
                                 </td>
                                 <td>
                                     <input type="file" id="intdoc" name="intdoc"><br>
                                     
                                 </td>
                             </tr>
							 
                             <tr>
                                 <td>
                                     Graduation Mark Sheet :
                                 </td>
                                 <td>
                                     <input type="file" id="ftcdoc" name="ftcdoc"><br>
                                     
                                 </td>
                             </tr>
                              
                             <tr>
                                 <td>
                                     Post Graduation Mark Sheet :
                                 </td>
                             
                            
                                 <td>
                                     <input type="file" id="fdmdoc" name="fdmdoc"><br>
                                     
                                 </td>
                             </tr>
                             <tr>
                                <td>
                                    Aadhar Card :
                                </td>
                             
                                <td><input type="file" id="fdcdoc" name="fdcdoc"><br>
                                 </td>
                             </tr>
                             
                            <tr>
                                <td>
                                    Cast Certificate :
                                </td>
                            
                                 <td>
                                    <input type="file" id="fide" name="fide"><br>
                                 
                                </td>
                            </tr> 
							
							<tr>
                                <td>
                                   Demand Draft's  Scanned Copy :
                                </td>
                            
                                 <td>
                                    <input type="file" id="ddide" name="ddide"><br>
                                 
                                </td>
                            </tr> 
                            
                            <tr>
                                <td>
                                    Signature
                                </td>
                            
                                 <td>
                                    <input type="file" id="fsig" name="fsig"><br>
                                 
                                </td>
                            </tr>
                            
                            <tr>
                                
                                <td colspan="2"><input type="submit" id="fpicup" name="fpicup" class="toggle btn btn-primary"></td>
                            </tr>
                           
                           </tbody>
                       </table>
        </div>
            <div class="container">
              <center> <div class="jumbotron" style="width:100%; box-shadow: -3px 3px 10px #999999;   margin-top:10px;">
                      <h3>
                          <b> <font style=" font-family: Verdana;  font-size:19px;"> Declaration By The Applicant</font></b>
                      </h3>
                           <p><font style=" font-family: Verdana;font-size:medium">
                                    I hereby solemnly declare that all the particulars given in this form are true to the best of my knowledge
                                    and belief. I shall abide by the rules and regulations laid down by the College from time to time.
                                    In case the particulars furnished by me are found false, my admission stands canceled.
                          
                                </font></p>
                           
                           <input type="checkbox" id="dec" name="dec" value="I accept" onclick="send()"><font style=" font-family: Verdana;font-size:medium">
                           I accept</font>
                               
                           
                         
                  </div>
            </div>
        </form>
    </body>
</html>

in this file i have included fileupload.php which supposed to upload the files. I want to rename all files while uploading. Presently when user upload a file it saves in upload dir and database but its later if another user upload a file with same name it overwrits the previous file of previous user. fileupload.ph is as follows. 

<?php
session_start();
$sp=mysqli_connect("localhost","root","","oas");
         if($sp->connect_errno){
                echo "Error <br/>".$sp->error;
}

$picpath="studentpic/";
$docpath="studentdoc/";
$proofpath="studentproof/";
$id=$_SESSION['user'];
if(isset($_POST['fpicup']))
{
$picpath=$picpath.$_FILES['fpic']['name'];
$docpath1=$docpath.$_FILES['ftndoc']['name'];
$docpath2=$docpath.$_FILES['intdoc']['name'];     
$docpath3=$docpath.$_FILES['ftcdoc']['name']; 
$docpath4=$docpath.$_FILES['fdmdoc']['name']; 
$proofpath1=$proofpath.$_FILES['fdcdoc']['name'];     
$proofpath2=$proofpath.$_FILES['fide']['name'];
$proofpath3=$proofpath.$_FILES['ddide']['name'];  
$proofpath4=$proofpath.$_FILES['fsig']['name']; 

if(move_uploaded_file($_FILES['fpic']['tmp_name'],$picpath)
  && move_uploaded_file($_FILES['ftndoc']['tmp_name'],$docpath1)
  && move_uploaded_file($_FILES['intdoc']['tmp_name'],$docpath2)
  && move_uploaded_file($_FILES['ftcdoc']['tmp_name'],$docpath3)
  && move_uploaded_file($_FILES['fdmdoc']['tmp_name'],$docpath4)
  && move_uploaded_file($_FILES['fdcdoc']['tmp_name'],$proofpath1)
  && move_uploaded_file($_FILES['fide']['tmp_name'],$proofpath2)
  && move_uploaded_file($_FILES['ddide']['tmp_name'],$proofpath3)
  && move_uploaded_file($_FILES['fsig']['tmp_name'],$proofpath4))
{

$img=$_FILES['fpic']['name'];
$img1=$_FILES['ftndoc']['name'];
$img2=$_FILES['intdoc']['name'];
$img3=$_FILES['ftcdoc']['name'];
$img4=$_FILES['fdmdoc']['name'];
$img5=$_FILES['fdcdoc']['name'];
$img6=$_FILES['fide']['name'];
$img7=$_FILES['ddide']['name'];
$img8=$_FILES['fsig']['name'];


$query="insert into t_userdoc (s_id,s_pic,s_tenmarkpic,s_twlmarkpic,s_grdmarkpic,s_pgmarkpic,
    s_idprfpic, s_cstpic, s_ddpic, s_sigpic) values 
    ('$id','$img','$img1','$img2','$img3','$img4','$img5','$img6','$img7','$img8')";
        if($sp->query($query)){
     echo "Inserted to DB ";    
    }else
    {
        echo "Error <br/>".$sp->error;        
    }
}
else
{
echo "There is an error,please retry or ckeck path";
}
}
 ?>

Please someone help me with this, Also i want to upload only few files type format. Like (.pne, .jpg, .jpeg, .pdf )

 

Thanks in advance.

Link to comment
Share on other sites

if you are storing uploaded files associated with each user, wouldn't using the user id as a leading part of the file name, followed by a separator character, then the file category, make sense? you can then find all the files associated with a user by doing a wild-card file match starting with the user's id and separator character.

Link to comment
Share on other sites

Hi, Sorry for replying late, I am newbie in php and mysql. learning through old scripts. 

 

I have just solved the renaming problem like this. Thanks to mac_gyver for pointing me in right direction. I have added id user id "$id " befor file name and it stors the image name after rename the actual.


<?php
session_start();
$sp=mysqli_connect("localhost","root","","oas");
         if($sp->connect_errno){
                echo "Error <br/>".$sp->error;
}

$picpath="studentpic/";
$matricmark="matricmark/";
$matriccert="matricert/";
$twlmark="twlmark/";
$twlcert="twlcert/";
$gradmark="gradmark/";
$gradcert="gradcert/";
$pgmark="pgmark/";
$pgcert="pgcert/";
$aadhar="aadhar/";
$caste="caste/";
$ddcopy="ddcopy/";
$usrsign="usrsign/";
$id=$_SESSION['user'];


if(isset($_POST['fpicup']))
{
$picpath=$picpath.$id.$_FILES['fpic']['name'];
$matricmark1=$matricmark.$id.$_FILES['ftndoc']['name'];
$matricert1=$matricert.$id.$_FILES['ftncert']['name'];
$twlmark1=$twlmark.$id.$_FILES['intdoc']['name'];  
$twlcert1=$twlcert.$id.$_FILES['intcert']['name'];    
$gradmark1=$gradmark.$id.$_FILES['ftcdoc']['name']; 
$gradcert1=$gradcert.$id.$_FILES['ftccert']['name']; 
$pgmark1=$pgmark.$id.$_FILES['fdmdoc']['name']; 
$pgcert1=$pgcert.$id.$_FILES['fdmcert']['name']; 
$aadhar1=$aadhar.$id.$_FILES['fdcdoc']['name'];     
$caste1=$caste.$id.$_FILES['fide']['name'];
$ddcopy1=$ddcopy.$id.$_FILES['ddide']['name'];  
$usrsign1=$usrsign.$id.$_FILES['fsig']['name']; 

if(move_uploaded_file($_FILES['fpic']['tmp_name'],$picpath)
  && move_uploaded_file($_FILES['ftndoc']['tmp_name'],$matricmark1)
  && move_uploaded_file($_FILES['ftncert']['tmp_name'],$matricert1)
  && move_uploaded_file($_FILES['intdoc']['tmp_name'],$twlmark1)
  && move_uploaded_file($_FILES['intcert']['tmp_name'],$twlcert1)
  && move_uploaded_file($_FILES['ftcdoc']['tmp_name'],$gradmark1)
  && move_uploaded_file($_FILES['ftccert']['tmp_name'],$gradcert1)
  && move_uploaded_file($_FILES['fdmdoc']['tmp_name'],$pgmark1)
  && move_uploaded_file($_FILES['fdmcert']['tmp_name'],$pgcert1)
  && move_uploaded_file($_FILES['fdcdoc']['tmp_name'],$aadhar1)
  && move_uploaded_file($_FILES['fide']['tmp_name'],$caste1)
  && move_uploaded_file($_FILES['ddide']['tmp_name'],$ddcopy1)
  && move_uploaded_file($_FILES['fsig']['tmp_name'],$usrsign1))
{

$img=$id.$_FILES['fpic']['name'];
$img1=$id.$_FILES['ftndoc']['name'];
$img2=$id.$_FILES['ftncert']['name'];
$img3=$id.$_FILES['intdoc']['name'];
$img4=$id.$_FILES['intcert']['name'];
$img5=$id.$_FILES['ftcdoc']['name'];
$img6=$id.$_FILES['ftccert']['name'];
$img7=$id.$_FILES['fdmdoc']['name'];
$img8=$id.$_FILES['fdmcert']['name'];
$img9=$id.$_FILES['fdcdoc']['name'];
$img10=$id.$_FILES['fide']['name'];
$img11=$id.$_FILES['ddide']['name'];
$img12=$id.$_FILES['fsig']['name'];


$query="insert into t_userdoc (s_id,s_pic,s_tenmarkpic,s_tencertpic,s_twlmarkpic,s_twlcertpic,s_grdmarkpic,s_grdcertpic,s_pgmarkpic,s_pgcertpic,
    s_idprfpic, s_cstpic, s_ddpic, s_sigpic) values 
    ('$id','$img','$img1','$img2','$img3','$img4','$img5','$img6','$img7','$img8','$img9','$img10','$img11','$img12')";
        if($sp->query($query)){
     echo "All files Uploaded successfully";    
    }else
    {
        echo "Error <br/>".$sp->error;        
    }
}
else
{
echo "There is an error,please retry or ckeck path";
}
}
 ?>

But now i have another problem, if user don't select any of the files to upload its showing error and not uploading rest of the documents not updating rest of the documents in database. mac_gyver can you sugest any code changes to overcome this problem. because not every documents is required.

Link to comment
Share on other sites

On 7/5/2018 at 4:45 PM, mac_gyver said:

if you are storing uploaded files associated with each user, wouldn't using the user id as a leading part of the file name, followed by a separator character, then the file category, make sense? you can then find all the files associated with a user by doing a wild-card file match starting with the user's id and separator character.

Thanks to for pointing me in right direction. I have added id user id "$id " befor file name and it stors the image name after rename the actual. But now i have another problem, if user don't select any of the files to upload its showing error and not uploading rest of the documents not updating rest of the documents in database. mac_gyver can you sugest any code changes to overcome this problem. because not every documents is required.

Link to comment
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.