3

Thanks in advance for any help, I hope my explanation of my request is understandable.

I have a website where I upload various HTML pages with scripts, websites etc. that I have found useful over time... For the purpose of 1) a reference for myself, and 2) to share what I've found with others.

The website consists of 2 sections. A search page to find the script, and an admin page to upload it. The uploaded HTML file gets placed in a "docs/" directory on my server, and the details are added to a MySQL database for the search page.

The form looks like this:

<form name="upload" enctype="multipart/form-data" action="includes/add.php" 
method="post" onsubmit="return validateForm();"> 

<label for="scriptname">Script Name</label><input class="inputarea" type="text" 
name="scriptname"><br> 
<label for="category">Category</label><input class="inputarea" type="text" 
name="category"><br> 
<label for="keywords">Keywords</label><input class="inputarea" type="text" 
name="keywords"><br>
<label for="content">HTML File</label><input class="inputarea" type="file" 
name="content"><br> 
<input class="submit" type="submit" value="Add"> 
</form>

My question is this... Is there any way with JavaScript or PHP to do the following:

  1. generate an automatic file name for the uploaded file (a few random digits would do)
  2. In the "scriptname" input field, add text on submit so that it makes the Script name and file name into a hyperlink that's added to the database as text... eg. When submit button is pressed, the following is added to the database:

    <a href="docs/automatic_file_name.html">"scriptname_input"</a>

Where the bold section is taken from the generated file name and the italic section is from the input field...

The purpose of this is so that in the search results, when the database column with the script name comes up, the script name is a link to the actual file. I have the search feature ready, and it is able to make a link from a database entry, but I just need to simplify the upload process.

If this is not possible, is there a different way to achieve this?

---EDIT---

Thank you all for your help! Much appreciated, I've worked it out using a combination of a few of the suggestions. However, I gave the credit to Ibere as his solution was the closest.

Here is the final code I used for the 'add.php' file that processed the upload and database addition, just in case it ever comes up again (I doubt it) :P

<?php

$filename = md5($_FILES['content']['name']);
$labelForUrl = $_POST['scriptname'];
$url = "<a href=\"docs/$filename\">$labelForUrl</a>";
$target = "../docs/";

//This gets all the other information from the form
$name=$_POST['scriptname'];
$cat=$_POST['category'];
$key=$_POST['keywords'];
$link=$_POST['link'];
$file=($_FILES['content']['scriptname']);

// Connects to your Database
mysql_connect("localhost", "username", "password") or
die(mysql_error()) ;
mysql_select_db("scripts") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO scripttable (scriptname,category,keywords,link,content)
VALUES ('$url', '$cat', '$key', '$link', '$file')") ;

if(move_uploaded_file($_FILES['content']['tmp_name'], $target . $filename)) {
echo "The file ".  $labelForUrl. 
" has been uploaded";
}
else {
    echo "There was an error uploading the file, please try again!";
    }

?>
1
  • These should be small changes to your current upload script. Can you post that script? Commented Sep 14, 2012 at 14:05

4 Answers 4

1

You can do something like this for the filename.

$filename = md5($_FILES['content']['name']);

$labelForUrl = $_POST['scriptname'];

md5 is not Random, but is good enough for generating a unreadable string for a filename.
Then you can create a url like this

<a href="docs/<?php echo $filename; ?>" ><?php echo $labelForUrl; ?></a>

Hope this helps.

EDIT: I forgot to add the extension to the filname. So the right code would be something like:

$filename = md5($_FILES['content']['name']).$_FILES['content']['type']
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers! With a few tweaks, I got this working PERFECTLY... Great help.
1

I recommend using uploadify for uploads. But, to do what you asked:

$randomFileName = rand(1000, 9999);
if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $randomFileName . $_FILES["file"]["type"]);
  // update your db with the location
  $loc = "upload/" . $randomFileName . $_FILES["file"]["type"];    
  mysqli_query("insert into `myTable` (`loc`) values ('$loc')");
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}

For file uploading help, look at http://www.w3schools.com/php/php_file_upload.asp

2 Comments

Thanks heaps, that's very close to what I need, however, I also wish for the script name from the form to be uploaded as a hyperlink, so that when the script is found in the search, the user will click on the script name and be taken to the uploaded html file... To clarify, the script name being what was inserted in the text field as opposed to the file name, which is set as random. Is this possible?
Are you trying to upload files pertaining to a web page, then generate or upload a web page assigning it a random name?
0

This is very easy, if you know codeigniter ( PHP Framework ).

You can use the Upload Class

You can easily create forms and submit them and also display them.

I would do it that way. If you are familiar with MVC you can do that in 10-15 mins.

Comments

0
  1. To generate random file names, I usually find this does the work quite well: md5( rand( 0, 100000 ) );. If you wish to limit the size of the file name, you may use the substr function.
  2. (Assuming a MySQL database), make the connection and then query the database using the INSERT command. This link shows how to do all of this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.