Jump to content

html image uploader


davidpc

Recommended Posts

On my website, users can upload images to me to post. Sometimes I could get several people to upload before I can get to them. I need a uploader that will keep them seperate. I have one that I have been working with, but if 2 people upload, files go into the same folder and are mixed up. Any ideas? I am attaching what I have been working with. I am not versed in html code at all as it has all been learned from trial and error...more error than trial

<html>
<head>
<title>Upload</title>
</head>
<body background="background.jpg">
<br><br><br>
<p align="center">
<img src="header.jpg" height="157" width="799">
<br><br><br><br><br><br>
<form action="upload.php" method="post" enctype="multipart/form-data">
   <font color="white">
<p align="center">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

 

Link to comment
Share on other sites

php that dynamically defines new input(s) you'd need to add to your form to id uploader and/or folder 

 

Edited by niche
Link to comment
Share on other sites

Ok. You guys are going to have to help me here. Show me the code that will do that. I am not familair with how to time stamp. And as I said I know html only by what I have learned and have never really worked with php as it seems more involved

Link to comment
Share on other sites

dsonesuk is absolutely right.  Going from html and scripts you might’ve found on the web to writing scripts for your use, based on your situation, is a big DOABLE step.

Else, you’ll have difficulty finding what you need off the self or will need to pay someone to write it for you.  The trouble with paying someone, when you don’t code is managing that project.  The days when someone with money and low skills can hire someone with skills are vanishing, especially if you want to be totally satisfied with your code project.

If your want to do this, your first job is to set up your localhost.  You can get it at wampserver.com.  IT’S FREE!

EDIT:

2nd job is to start the php and mysql tutorials so you can ask informed questions.  People that can show a little diligence frequently get enough code, from the forums to get ‘em over their current challenge, but that’s not a guarantee.

Qapla!

Edited by niche
Link to comment
Share on other sites

Ok. Here is what I have now. It works, but if 2 people upload before I get to it, they get mixed up. This is the main .html file.

<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

I have a php.ini and all it says is "file_uploads = On" with out brackets. Last is the .php file


<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

In the second line, it tells where to place the files, the uploads directory. I need a way that 2 or more people can upload without things getting jumbled up 

Link to comment
Share on other sites

Basically, you're going to need to decide how you're going get mkdir() into your script or something like it.  You're dancing around the concept of handling sessions whether of not you actually use the $_SESSION array.   

EDIT:

Or, modify the uploaded filename to get user id in it.

Either way, you'll need to sanitize user supplied inputs and probably need to add one or more inputs to your form.  Best to remember what dsonesuk said.

 

 

Edited by niche
Link to comment
Share on other sites

I'm sorry. I have no idea what you mean. You are over my head. As I said, I have a very basic understanding. I have no idea how to handle a session or use $_SESSION array. And no idea how to use FILTER_SANITIZE_STRING. Most of what I posted here was given to me by a friend, but he had no idea how to get around my "problem"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...