Jump to content

[SOLVED] Verify, rename, and upload


JackW

Recommended Posts

I have been trying for days to write a code that will upload a picture to my server after renaming it, verifying that it is a picture and not oversized, then add a link to my data base.  What I have put together works fine when all requirements are met, however if I substitute a file that is not a picture (example .txt)  page 2 either locks up as a blank page or uploads the incorrect file to the site. No error message of any kind. What am I doing wrong?
Your help will be greatly appreciated.  Here is my code:

Page 1

<html>
<body>
<form action="add6.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Upload Picture" />
</form>
</body>
</html>

Page 2

<html>
<body>

<?php


if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 20000))

  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
   
  else
  {

//This function separates the extension from the rest of the file name and returns it

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file

$ext = findexts ($_FILES['file']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.

$ran = rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "/My_site_info/uploads/";
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
$image = $ran2.$ext;
//Writes the photo to the server



if(move_uploaded_file($_FILES['file']['tmp_name'], $target))

{
echo "The file has been uploaded as ".$ran2.$ext;

echo '<form  action="putin.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
<input type="hidden" name="picture"value="';
echo$image;
echo '">' ;
echo'<input type="submit" value="Submit">
</form>';

}

else

{

echo "Sorry, there was a problem uploading your file.";
}

}
}

?>
</body>
</html>

Page 3

<html>
<body>
<?php

$username="Myusername";
$password="mypasswordy";
$database="mydatabase";
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=$_POST['picture'];

// Connects to your Database
@$db = mysql_pconnect('myinfo,'mydatabase','mypasswordy');
mysql_select_db('mydatabase') or die( "Unable to select database");

//Writes the information to the database
{
mysql_query("INSERT INTO `mytable` VALUES (NULL,'$name', '$email', '$phone', '$pic')");
}

$results=mysql_query($query);
{
echo 'Thank you for Submitting your information;
}
mysql_close($db);
?>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/29968-solved-verify-rename-and-upload/
Share on other sites

I need more help.  This is what I have now.  If I leave the else statment out it tells me I have an unexpetected T function.  With it in I get an error of unexpected else.  I probably don't understand the die function. I am really getting frustrated.

<?php


if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 20000))

  {
if ($_FILES["file"]["error"] > 0)die ( "Sorry, there was a problem with your file. It may not be a picture file or is larger than 4k.")

    else  [color=red]//(if I leave this else out I get an error of unexpected T_function with it in I get an error of unexpected else)[/color]

//This function separates the extension from the rest of the file name and returns it

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file

$ext = findexts ($_FILES['file']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.

$ran = rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "/My_site_info/uploads/";
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
$image = $ran2.$ext;
//Writes the photo to the server
}

if(move_uploaded_file($_FILES['file']['tmp_name'], $target))

{
echo "The file has been uploaded as ".$ran2.$ext;

echo '<form  action="putin.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
<input type="hidden" name="picture"value="';
echo$image;
echo '">' ;
echo'<input type="submit" value="Submit">
</form>';

}
else

{

echo "Sorry, there was a problem uploading your file.";
}

?>
  • 4 weeks later...
Finally it works.  Here is the code in 3 pages that works for me. Hope it well be of help to someone.

[color=teal]First page is html[/color]
<body>
<form enctype="multipart/form-data" name="picture" onSubmit="return formVerify(this)" action="add5.4.php" method="post">
<!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="82000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="file" type="file" />
<br /><br />
    <input type="submit" value="Send File" />
</form>
</body>

[color=teal]Next page is php[/color]
<body>
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/jpg"))
{
echo 'That is valid picture file.<br>';
}
else
    {
    echo 'There is an error in your file!  Your file must be a jpg or gif format and no larger that 80k';
exit;
    } 
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['file']['name']) ;
//This line assigns a time stapm to a variable. You could also use a random number here if you prefer.
$ran = time() ;
//This takes the timestamp (or random number) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";
//This assigns the subdirectory you want to save into... make sure it exists!
//find this path using phpinfo.php
$target = "/home/content/y/o/u/yoursite/html/uploads/";
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
$image = $ran2.$ext;
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "Your picture file has been renamed and uploaded as ".$ran2.$ext;
echo '<br>Please complete the form below and click submit to post your listing.';
echo '<form  action="putin.php" method="POST">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Phone: <input type="text" name = "phone"><br>
<input type="hidden" name="picture"value="';
echo$image;
echo '">';
echo '<input type="submit" value="Submit">
</form>';
}
else
{
echo 'Sorry, there was a problem uploading your file.';
}
?>
</body>

[color=teal]Final page is php and puts it together.[/color]
<body>
<?php
$username="your_data";
$password="your_password";
$database="your_data_base";
//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=$_POST['picture'];
// Connects to your Database
@$db = mysql_pconnect('your_data','your_password','your_data_base');
mysql_select_db('yourtable') or die( "Unable to select database");
//Writes the information to the database
{
mysql_query("INSERT INTO `yourtable` VALUES (NULL,'$name', '$email', '$phone', '$pic')");
}
$results=mysql_query($query);
{
echo 'Thank you for your Listing';
}
mysql_close($db);
?>
</body>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.