Jump to content

Recommended Posts

Hello.

Well, I'm having quick a bit of trouble with this.

What I'm trying to do is, this is a file-uploading script.

This uploads all of the details of the file to the sql database, and the file to a folder.

What I want to do, is when it moves the file over to the folder, I want a string to generate a random name for that file, but keep everything the same, like the contents and the format of that file example .PHP

Why do I want to do this?

Because if you have 2 files with the same name, you upload the first, then upload the second, the second completely replaces the first. So someone could loose something they needed.

 

So, any help?

Here is my code so fare:

 

<?php
$db = mysql_pconnect($dbhost,$dbuser,$dbpass);
mysql_select_db("$dbname",$db); 
$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "<br>Files uploaded<br>";

}

 

Thanks?

 

Link to comment
https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/
Share on other sites

 

This solves the problem which I don't have of Unique ID's in the database, but I was more looking for a random function/string to be applied on the name of the file in the folder :)

Which I can't figure out how to do.

Did you bother to read the information at the link that was posted?

 

Yes.

But I could easily make a random name generator.

Its applying it to the name of the file that is uploaded.

 

Then, on this line.....

 

$filePath = $uploadDir . $fileName;

 

Replace $fileName with your randomly generated file name.

It worked, yay!

But now, it got worse.

It replaced the format of the file that was uploaded to x-generic of the file.

In the SQL database it still says (I uploaded a PHP file) it still says its a PHP file, but the path is just the random string, and the file in the folder is the same.

Help?

Its not rocket science. The variables you put in the database need to remain intact. eg; Don't override $fileName with your random string.

 

The database still recognizes the PHP file as a PHP file tho..

 

4	post.php	application/x-php	26	uploads/7kffyqjv

 

Thats what it looks like.

 

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.