Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Is there a way I can change my script to auto name the file when its being stored? Maybe by numbers lets say each time I upload a picture it should rename it to the number after the previous number.

like example"
Upload 1: 1.jpg
Upload 2: 2.jpg etc..

My current code I have is as follow

Form.html

PHP
<html>
<body><br>
<h1>My upload file form.</h1>


<form enctype="multipart/form-data" action="process.php" method="post">


Enter File Name:<input type="text" name="name" value="Please enter file name:" /><br>


Enter Description:<input type="text" name="desc" value="Description:" /><br>


Select image:<input type="file" name="prodImg"><br>


<input type="submit" value="Submit Page" />


</form>
<?php


And my processing script

process.php

PHP
<?php


extract($_POST);


$name;


$desc;


$fileType = $_FILES['prodImg']['type'];
 $fileSize = $_FILES['prodImg']['size'];


if($fileSize/1024 > '150') {
 echo 'Filesize is not correct it should equal to 2 MB or less than 2 MB.';
 exit();
 } //FileSize Checking


if($fileType != 'image/gif' &&
 $fileType != 'image/jpg' &&
 $fileType != 'image/jpeg'


 )     {
 echo 'Sorry this file type is not supported we accept only. Jpeg, Gif, PNG, or ';
 exit();
 } //file type checking ends here.
 $upFile = 'files/galaxywars/'.date('Y_m_d_H_i_s').$_FILES['prodImg']['name'];


if(is_uploaded_file($_FILES['prodImg']['tmp_name'])) {
 if(!move_uploaded_file($_FILES['prodImg']['tmp_name'], $upFile)) {
 echo 'Problem could not move file to destination. Please check again later. <a href="index.php">Please go back.</a>';
 exit;
 }
 } else {
 echo 'Problem: Possible file upload attack. Filename: ';
 echo $_FILES['prodImg']['name'];
 exit;
 }
 $prodImg = $upFile;
  //File upload ends here.


$upFile;
echo "Thank you for sending your file";


 ?>
<?php






include "menu3.php";


?>


Can anybody assist me please...
Posted

1 solution

I think better use GUID as file name while storing. Create a table where you can store GUID and corresponding original file name. It will help you in future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900