Jump to content

Recommended Posts

<?php

$q = mysql_query("SELECT id FROM table WHERE filename = '$filename'") or die(mysql_error()); // Select a single value from the table
$rows = mysql_num_rows($q) or die(mysql_error()); // Then count the rows

if($rows > 0) { // If filename already exists
$filename .= "abc"; // Add abc to the end of it (you may need to explode() the filename to add it before the extension
}

?>

Link to comment
https://forums.phpfreaks.com/topic/52178-php-upload-script/#findComment-257395
Share on other sites

I'm sorry, but I'm confused.  If this is my script, what would I have to do?

<?php    
require_once('config.php');
if(empty($_POST['submitBtn'])){

echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'DTD/xhtml1-transitional.dtd'>
<html>
<body>
      <form action='upload.php' method='post' name='fileForm' enctype='multipart/form-data'>
        File to upload:
        <table>
          <tr><td><input name='upfile' type='file'></td></tr>
          <tr><td><input type='submit' name='submitBtn' value='Upload'></td></tr>
        </table>  
      </form>
</body>";

}
    if (isset($_POST['submitBtn'])){

        // Path to upload the file
        $target_path = "uploads/";

        // Create the file name with path
        $target_path = $target_path . basename( $_FILES['upfile']['name']); 
     
        $filename = basename( $_FILES['upfile']['name']);

             $findimage = mysql_query("SELECT * FROM recent_files WHERE filename = $filename") or die("Error ".mysql_error());

$imagesfound = mysql_num_rows($findimage) or die(mysql_error()); 

     //If an image with the filename was found in the database...
     if($imagesfound != 0){

          //Generate 5 random characters to place before the file extension
          function generate_filename($length)
{
    $string = md5(time());
    $highest_startpoint = 5-$length;
    $filename = $filename .substr($string,rand(0,$highest_startpoint),$length);

    return $filename;

}

        // Try to move the file from the temporay directory to the defined.
        if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)){

  //Generate session id for that user
  function generate_session($length)
{

    $string = md5(time());
    $highest_startpoint = 32-$length;
    $session = substr($string,rand(0,$highest_startpoint),$length);

    return $session;

}

    $session = generate_session(10);

//Insert the image into the database
$query = mysql_query("INSERT INTO recent_files (filename,session)
                values('$filename', '$session')") or die("Error: ".mysql_error());
                     $_SESSION['mysession'] = $randomString;
            header("Location:image.php?img=$filename");
        } else{
            echo "There was an error uploading the file, please try again!";
        }
    }
?>

Link to comment
https://forums.phpfreaks.com/topic/52178-php-upload-script/#findComment-257702
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.