Jump to content

[SOLVED] PHP file uploader


spikypunker

Recommended Posts

Hi,

 

I have been trying to construct a simple image uploader for a news item entry. The user selects a news item from the list, then on the same page they select a file to upload. I've had two problems wtih this so far and no one else to ask for help :o( lol

 

Firstly this is how I've changed the basic script to allow each image to be saved with the ID of the news item as the filename.

I.e News item 5 would have newsno = 5 so the image would be saved as 5.gif. I've not worried about verification at this stage, which i obviously will after getting the basics to work but security wise this site cannot be accessed by the public so there should be no risk.

 

 

addimage.php

 

<body>

<form method="post" action="selectimage.php">
<span class="style1">First select the News Item you wish you add an image to...</span><br />
<br />
<?
mysql_connect("db1", "stanga_1", "*********"); 
@mysql_select_db("stanga_1") or die ("unable to connect"); 
$query = "SELECT * FROM news";
$result = mysql_query($query); 
$num = mysql_num_rows($result);
mysql_close();
$i = 0;


while ($i < $num)
{ 
$newsno = mysql_result($result,$i,"newsno");
$date = mysql_result($result,$i,"date");
$head = mysql_result($result,$i,"head");
$body = mysql_result($result,$i,"body");
?>

<input name="item" type="radio" id="item" value="<? echo $newsno;    ?>">

<? echo " <span class='style2'>$head</span><br>";


$i++;
}
?>

<br /><Br /><br />
<span class="style1">Now Select the Image you wish to upload</span>...<br /><br />
<input name="uploadFile" type="file" /><br /><br />
<input type="submit" value="Add Image"/>
</form>
</body>

 

 

selectimage.php


<? 

$id = $_POST['item'];

echo ($id);

$target_path = "uploads/";

$target_path = $target_path . basename( '$id'.gif);  [color=red]THIS IS THE BIT NOT TOO SURE ABOUT![/color]

if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) {
    echo "The file ".  basename('$id'.gif). [color=red]THIS IS THE BIT NOT TOO SURE ABOUT![/color]
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


?>


 

Secondly, This is the original basic work-through which simply saves the file with the original name, not using the news ID, THIS ISNT WORKING EITHER!!!! Which is my main concern really, i can't actually upload a file at all!

 

selectimage.php

<? 

$id = $_POST['item'];

echo ($id);

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


?>

 

 

Link to comment
Share on other sites

kindly note

 

The code in second code snippet

$id = $_POST['item'];

 

echo ($id);//you have fetched the id

 

$target_path = "uploads/";

 

$target_path = $target_path . basename( '$id'.gif);  echo $target_path and see what it prints does it print uploads/$id.gif or uploads/1.gif?????

Link to comment
Share on other sites

Hi thanks for reply,

 

 

Yeah for some reasons it's printing

 

uploads/$id.gif 

 

I dont know why??

 

Can anyone help? More important is the fact that it's not uploading anything at all on the normal upload form?? I've checked with the hosting provider and they say they have no restrictions on php upload forms apart from a 6mb upload limit, the test file i am using is about 2kb!

Link to comment
Share on other sites

I just tried these:

 

$target_path = $target_path . basename( $id".gif"); 

 

And got this:

 

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /mnt/vol1/home/s/t/stanga/public_html/selectimage.php on line 27

 

 

 

When i try this:

 

$target_path = $target_path . basename( $id '.gif'); 

 

I get this:

 

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /mnt/vol1/home/s/t/stanga/public_html/selectimage.php on line 27

 

 

Link to comment
Share on other sites

Hi, cheers for helping.

 

Right here it is to date.

 

addimage.php

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add Image</title>
<style type="text/css">
<!--
.style2 {font-size: 12px}

.style1 {font-size: 14px}
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #000000;
}
a:visited {
color: #000000;
text-decoration: none;
}
a:hover {
color: #000000;
text-decoration: underline;
}
a:active {
color: #000000;
text-decoration: none;
}
a:link {
text-decoration: none;
}
-->
</style>
</head>

<body>

<form enctype=multipart/form-data method="post" action="selectimage.php" >
<?
mysql_connect("db1", "stanga_1", "pencil"); 
@mysql_select_db("stanga_1") or die ("unable to connect"); 
$query = "SELECT * FROM news";
$result = mysql_query($query); 
$num = mysql_num_rows($result);
mysql_close();
$i = 0;


while ($i < $num)
{ 
$newsno = mysql_result($result,$i,"newsno");
$date = mysql_result($result,$i,"date");
$head = mysql_result($result,$i,"head");
$body = mysql_result($result,$i,"body");
?>

<input name="item" type="radio" id="item" value="<? echo $newsno;    ?>">

<? echo " <span class='style2'>$head</span><br>";


$i++;
}
?>


<br /><Br /><br />
<span class="style1">Now Select the Image you wish to upload</span>...<br /><br />
<input name="uploadFile" type="file" /><br /><br />
<input type="submit" value="Add Image"/>
</form>
</body>
</html>

[\code]



selectimage.php

[code]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Delete</title>
<style type="text/css">
<!--
.style1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</head>

<body>


<span class="style1">
<? 
$id = $_POST['item'];

echo $_FILES["file"]["tmp_name"];


$id = $id . ".gif";


$target_path = "uploads/";

$target_path = $target_path . basename( $id); 


if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


?>





</body>
</html>


[\code]


At the moment it's passing the variable fine, creating the right file path ie "uploads/3.gif" but not uploading the file at all?
I've contacted the hosters and they have nothing blocking php uploads providing the file is no bigger than 6 mb.

Thanks for your time!

[/code]

Link to comment
Share on other sites


<?php $path = dirname(__FILE__);
$destination= $path;



if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
else
{
move_uploaded_file ($_FILES["file"]["tmp_name"] , $destination. $_FILES["file"]["name"]);
    echo "Upload Successful";

}

Link to comment
Share on other sites

use this form as well

 

<html>
<body><form action="uploader.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="Submit" />
</form></body>
</html> 

 

also echo out

 

$destination and see where the file is supposed to be going. Also echo $_FILES["file"]["tmp_name"]

 

Link to comment
Share on other sites

hi, ok i tried that new form, with the echos you asked for.

 

If i try:  echo "$_FILES["file"]["tmp_name"]";

 

I get:

 

Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /mnt/vol1/home/s/t/stanga/public_html/uploader.php on line 22

 

And when i try: echo " $destination";

 

I get:

 

mnt/vol1/home/s/t/stanga/public_html  (which is root folder)

Link to comment
Share on other sites

Line 22 is the echo "$_FILES["file"]["tmp_name"]";

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Delete</title>
<style type="text/css">
<!--
.style1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</head>

<body>

<span class="style1">
<?php $path = dirname(__FILE__);
$destination= $path;
echo "$_FILES["file"]["tmp_name"]";


if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
else
{
move_uploaded_file ($_FILES["file"]["tmp_name"] , $destination. $_FILES["file"]["name"]);
    echo "Upload Successful";

}

?>

</body>
</html>

Link to comment
Share on other sites

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.