Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with file Uploads

Status
Not open for further replies.

jgd12345

Technical User
Joined
Apr 22, 2003
Messages
124
Location
GB
My server runs in safe mode but I don't think it's a safe mode restriction. Basically what I've got is a simple for with the enctype thing in the form bit and one of the fields being <input type=&quot;file&quot; name=&quot;location&quot;> but I'm receiving the following error:

Warning: stat failed for images/1.jpg (errno=2 - No such file or directory) in /home/virtual/site56/fst/var/ on line 1001

but the directory does exist. Here's the part which does the upload:

if ($location == &quot;&quot;) {
exit('Error: No file selected to upload');
} else {
$test = $HTTP_POST_FILES[location][name];
$test = substr($test, -4);

if ($test == &quot;.jpg&quot; XOR $test == &quot;.png&quot; XOR $test == &quot;.gif&quot; XOR $test == &quot;.JPG&quot; XOR $test == &quot;.PNG&quot; XOR $test == &quot;.GIF&quot; XOR $test == &quot;jpeg&quot; XOR $test == &quot;JPEG&quot;) {
if ($test == &quot;JPEG&quot; xor $test == &quot;jpeg&quot;) {
$test = &quot;.jpeg&quot;;
}

$image = &quot;images/1$test&quot;;

move_uploaded_file($location, &quot;$image&quot;);
} else {
exit('Error: Wrong File Type');
}

echo 'Successfully added';
}

I'd be greatful for your help. Thanx
 
I think the problem is that there is no tempory file specified that he must upload/move:

Try this:

$upload_file = $_FILES['location']['name'];
$temp = $_FILES['location']['tmp_name'];
$extension = substr($upload_file,-4);

and then:
$image = &quot;images/1&quot;.$extension;
move_uploaded_file($temp, $image);

Don't eat yellow snow!
 
Cheers worked a treat. I was also wondering how you create a thumbnail. Currently I resize the image using the following but that keeps the same file size ie 70kb. I was wondering if there was something I can do make it smaller. Thanx

if($image[thumbnail] == &quot;no&quot;) {
$image[thumbnail] = &quot;$image[imageurl]&quot;;
} else {
$image[thumbnail] = str_replace(&quot; &quot;, &quot;%20&quot;, $image[imageurl]);
$imageurl = $image[imageurl];

$maxw = 80; // Height of Thumbnail
$maxh = 80; // Width of Thumbnail

$SizeArray = getimagesize($imageurl);

if($SizeArray[0] > $maxw || $SizeArray[1] > $maxh) {
$width = $SizeArray[0];
$height = $SizeArray[1];

$percent = $maxh / $width;
$height = $height * $percent;

if($height > $maxh) {
$percent2 = $maxw / $SizeArray[1];
$SizeArray[0] = $SizeArray[0] * $percent2;
$w = 'width=&quot;'.$SizeArray[0].'&quot; height=&quot;'.$maxh.'&quot;';
} else {
$w = 'width=&quot;'.$maxw.'&quot; height=&quot;'.$height.'&quot;';
}
} else {
$image[thumbnail] = str_replace(&quot; &quot;, &quot;%20&quot;, $image[thumbnail]);
}
}
 
Had a read and tried a few things, but didn't really understand it. Thanx anywayz. Do you know of a built in function or one you could let me use which compresses the size of an image (ie if it was located at images/name.gif and its size was 70kb makes it 2kb) and then creates that image aswell. Thanx
 
Hello jgd12345,

I wanted to kow if you can provide me with the upload and view image script that you are using, I have tried to get help on this forum but it seems like everyone exspets you to understand there tech language. if you may can you please post it here as well..

Thanks.

Max
 
Yeah I get the same problems. Here's a basic example of what I'm using:

if($action == &quot;newimage&quot;) {
if(!$submit) {
<br>
<form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; action=&quot;edit.php?action=newimage&quot; name=&quot;input&quot;>
<table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; align=&quot;center&quot; class=&quot;table&quot;>
<tr>
<td bgcolor=&quot;$bordercolor&quot;>
<table cellspacing=&quot;$borderwidth&quot; cellpadding=&quot;$tablespace&quot; align=&quot;center&quot; width=&quot;100%&quot;>
<tr>
<td colspan=&quot;2&quot; class=&quot;header&quot;>New Image</td>
</tr>
<tr>
<td bgcolor=&quot;$altbg1&quot; class=&quot;tablerow&quot;>Location:</td>
<td bgcolor=&quot;$altbg2&quot; class=&quot;tablerow&quot;><input type=&quot;file&quot; name=&quot;location&quot; class=&quot;text&quot;></td>
</tr>
<tr>
<td colspan=&quot;2&quot; align=&quot;center&quot; bgcolor=&quot;$altbg1&quot; class=&quot;tablerow&quot;><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;submit&quot;></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
} else {
$location = checkInput($location, '', '', 'javascript');

$query = mysql_query(&quot;SELECT id FROM $table_media_images ORDER BY id DESC LIMIT 1&quot;);
$nb = $db->fetch_array($query, 0);

$filename = $nb[id] + 1;

if ($location == &quot;&quot;) {
exit('Error: No file selected to upload');
} else {
$upload_file = $HTTP_POST_FILES['location']['name'];
$temp = $HTTP_POST_FILES['location']['tmp_name'];
$extension = substr($upload_file, -4);

if ($extension == &quot;.jpg&quot; XOR $extension == &quot;.png&quot; XOR $extension == &quot;.gif&quot; XOR $extension == &quot;.JPG&quot; XOR $extension == &quot;.PNG&quot; XOR $extension == &quot;.GIF&quot; XOR $extension == &quot;jpeg&quot; XOR $extension == &quot;JPEG&quot;) {
if ($extension == &quot;JPEG&quot; xor $extension == &quot;jpeg&quot;) {
$extension = &quot;.jpeg&quot;;
}

$imageurl = &quot;images/$filename$extension&quot;;

move_uploaded_file($temp, $imageurl);
} else {
exit('Error: Wrong File Type');
}

echo 'Successfully added';
}

mysql_query(&quot;INSERT INTO $table_media_images VALUES('', '$imageurl', '', '', '', '')&quot;);

$outputtext = &quot;Image successfully added!&quot;;
eval(&quot;\$output = \&quot;&quot;.template(&quot;output&quot;).&quot;\&quot;;&quot;);
echo $output;/*
?>

<script>
function redirect() {
window.location.replace(&quot;edit.php?action=newimage&quot;);
}
setTimeout(&quot;redirect();&quot;, 1250);
</script>

<?*/
}
}

To display the images I've just done a loop through the records:

$query = mysql_query(&quot;SELECT * FROM $table_media_images&quot;);

while($image = mysql_fetch_array($query)) {
if($image[thumbnail] == &quot;no&quot;) {
$image[thumbnail] = &quot;$image[imageurl]&quot;;
} else {
$image[thumbnail] = str_replace(&quot; &quot;, &quot;%20&quot;, $image[imageurl]);
$imageurl = $image[imageurl];

$maxw = 80; // Height of Thumbnail
$maxh = 80; // Width of Thumbnail

$SizeArray = getimagesize($imageurl);

if($SizeArray[0] > $maxw || $SizeArray[1] > $maxh) {
$width = $SizeArray[0];
$height = $SizeArray[1];

$percent = $maxh / $width;
$height = $height * $percent;

if($height > $maxh) {
$percent2 = $maxw / $SizeArray[1];
$SizeArray[0] = $SizeArray[0] * $percent2;
$w = 'width=&quot;'.$SizeArray[0].'&quot; height=&quot;'.$maxh.'&quot;';
} else {
$w = 'width=&quot;'.$maxw.'&quot; height=&quot;'.$height.'&quot;';
}
} else {
$image[thumbnail] = str_replace(&quot; &quot;, &quot;%20&quot;, $image[thumbnail]);
}
}

echo '<img src=&quot;'.$image[thumbnail.'&quot; $w><br>';
}
 
cs7536:
jbd12345:
Tek-Tips is not a code repository.
Tek-Tips is not a free code-writing service.
Tek-Tips is not a free technical support service.

Tek-Tips is, as the text in the top-left of every page in the site attests, as set of technical work forums for computer professionals. As such, its purpose is to promote deeper understanding of the subjects in the site. Implicit in that social contract is effort on both the part of the questioner and the answerer.

If you have specific questions, the membership will answer them to the best of their abilities. But you must make it known if you do not understand the answers.


Want the best answers? Ask the best questions: TANSTAAFL!
 
I understand what your saying, However I am buying books and books on php along with php.net and tek-tips to teach my self php and I for the most part am coming along it is just the uploading and displaying an image where I can not grasp the idea. I just need minor help here. I know that what I am doing wrong is a little simple thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top