Hey, I'm reletively new to PHP (Used to learn it many years ago, just started again about a week ago) and I'm trying create an image uploader for a photo album I've created. What I'd like to do is create the upload script so it uploads the files to 'uploads/' but also adds the image name to a MySQL database. I've already got the database set up, and I've got the while loop set up to pull the images from the database and display them on screen, however I input the data into MySQL manually.

What would one have to do to create a script that does it on upload?

I've hit a brick wall with this so any help would be greatly appreciated.

Thank you for your time! <3

    Image uploading can be quite simple if you are doing it just for yourself. If you plan to let just any old visitor upload images, you have a lot of stuff to think about:
    making sure it's an image and not malware
    making sure it's not portn
    making sure the image is not too big
    etc.

    Do you have any idea how to create the image upload form and script? The MYSQL part is pretty simple once you have that.

      Thanks for the reply.

      Yeah I'm just creating the script as we speak, limiting size and making sure it's a jpg/png/gif etc.

        You may as well take a look at what I posted yesterday. just change the variable being stored from only the extension to the entire url.

          Uploading the avatar you mean?

          I've got the same problem again from my previous post.

          While creating the upload script, it goes to "Username and password required" when I click upload. I wonder if turning it into a switch instead of an if statement would solve it, not too clued up on switches though.

            How would you turn the following into a switch statement?

            <?php
            
            session_start();
            require("includes/connect.php");
            
            $post_username = $_POST['username'];
            $post_password = $_POST['password'];
            
            if($post_username && $post_password)
            {
            	if(strlen($post_username)>25 || strlen($post_password)>25)
            	{
            		die("Username or password is too long!");
            	}
            	else
            		{
            			//convert password to md5
            
            		$post_password = md5($post_password);
            
            		//query the database
            		$login = sprintf("SELECT * FROM users WHERE username='%s' AND password='%s'", mysql_real_escape_string($post_username), mysql_real_escape_string($post_password));
            
            		$rowcount = mysql_num_rows(mysql_query($login));
            
            		$fieldarray = mysql_fetch_assoc(mysql_query($login));
            
            		$id = $fieldarray['id'];
            
            		if($rowcount == 1)
            		{
            			$_SESSION['user']=$post_username;
            			$_SESSION['id']=$id;
            			echo $_SESSION['user'].", you have been logged in! <a href='index.php'>Back to main page</a>";
            		}
            		else
            		{
            			die("Incorrect username or password!");
            		}
            	}
            }
            else
            	die("Username and password required!");
            
            include('includes/analytics.php');
            ?>

              Well, this is your basic upload script. Taken mostly from the tutorial over at W3, but adapted to fit my needs. If you wanted 1) upload it to the server and 2) capture the name of the file in the database, i would do something like this:

              <?php
              //Determine variables
              $photo_dir = 'uploads/';
              $temp_name = $_FILES["name"]["tmp_name"];
              
              // Determine file types and size
              if ((($_FILES["name"]["type"] == "image/gif")
              || ($_FILES["name"]["type"] == "image/jpeg")
              || ($_FILES["name"]["type"] == "image/pjpeg")
              || ($_FILES["name]["type"] == "image/png"))
              && ($_FILES["name"]["size"] < 60000)){
              
              //Notify if error
              if ($_FILES[$name]["error"] > 0){
                  	echo "Return Code: " . $_FILES["name"]["error"] . "<br />";
              }else{
              
              // Upload it to server and insert into database
                  move_uploaded_file($tmp_name, $photo_dir.$tmp_name);
                  mysql_query("INSERT INTO 'photo_table' (photo_name) VALUE ($tmp_name);
              	}    
              //Kick out }else{ echo 'Invalid file,'; } ?>
                Write a Reply...