Jump to content

[SOLVED] Need Help! Upload jpg images.


cyrixware

Recommended Posts

Hi, is ther any idea how to upload jpg images. Let say for exmple, I have a text box and browse button.After  i click the button that says upload the image will upload. Then when i click the button view the images will view. How can i do that? I need your help... pls send me a codes for that...

 

And also i have a question uhmm... where is that images save? is ther a folder that all images will save or the images will save on the dbase?

 

Regards,

 

- Cyrixware ;)

Link to comment
Share on other sites

html form:

 

<form method="POST" enctype="multipart/form-data" action="playwith.php">
<p>
<input type="file" name="userfile" size="20">
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</p>
</form>

 

PHP handler

 

<?php
$filesize = $_FILES['userfile']['size']; 
$filetype = $_FILES['userfile']['type']; 
$nameuse = filename;
IF (($filesize <= 102000) AND ($filetype == 'image/pjpeg') OR ($filetype == 'image/jpeg'))
{

$nameuse = "$nameuse.jpg";
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/home/yoursection/public_html/uploads/';
$uploadfile = $uploaddir . $nameuse;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
	{

   		echo "File is valid, and was successfully uploaded.\n";
	} 
else 
	{
   		echo "Possible file upload attack!";
	}
}
IF (($filetype != 'image/pjpeg') AND ($filetype != 'image/jpeg'))
	{
   		echo "Your Image is the wrong file type, it must be a jpg image<BR>your image is $filetype";
	}
IF ($filesize > 102000)
{
echo "Your Image is to large to upload";
}
?> 

Link to comment
Share on other sites

thanks for the reply... should i create a single image folder directory  for the locations of the images uploaded?

 

or should i creat this directory?

 

$uploaddir = '/home/yoursection/public_html/uploads/';

 

:)

 

i use localhost in my pc...

 

ex:

 

http://localhost/uploads/playwith.php

 

then uploads folder :

 

http://localhost/uploads/uploads

 

 

 

Link to comment
Share on other sites

ok i get it now.. the problem is that when i try to upload another image again the previous image deleted itself automatically... meaning in a sigle folder only 1 image can be stored.. how can i store more than a single image? thanks again

 

Regards,

Cyrix

 

html form:

 

<form method="POST" enctype="multipart/form-data" action="playwith.php">
<p>
<input type="file" name="userfile" size="20">
<input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</p>
</form>

 

PHP handler

 

<?php
$filesize = $_FILES['userfile']['size']; 
$filetype = $_FILES['userfile']['type']; 
$nameuse = filename;
IF (($filesize <= 102000) AND ($filetype == 'image/pjpeg') OR ($filetype == 'image/jpeg'))
{

$nameuse = "$nameuse.jpg";
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/home/yoursection/public_html/uploads/';
$uploadfile = $uploaddir . $nameuse;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) 
	{

   		echo "File is valid, and was successfully uploaded.\n";
	} 
else 
	{
   		echo "Possible file upload attack!";
	}
}
IF (($filetype != 'image/pjpeg') AND ($filetype != 'image/jpeg'))
	{
   		echo "Your Image is the wrong file type, it must be a jpg image<BR>your image is $filetype";
	}
IF ($filesize > 102000)
{
echo "Your Image is to large to upload";
}
?> 

Link to comment
Share on other sites

Change this line of code:

 

$nameuse = filename;

 

... to ...

 

$nameuse = $_FILES['userfile']['name'];

 

... and that will stop your previous file from being overwritten. With the code the way it was, it was always naming the file "filename".

Link to comment
Share on other sites

as Heyray said, you want to change that line, but, if you have random people uploading its possible for them to overwrite each others files when they upload... the way I have it on my actual page is I use a timestamp basically for the file name, looks like this

 

$nameuse  = gmdate("YmdHis");

 

This way, the only way you get two with the same name is if they are posted at the exact same second

Link to comment
Share on other sites

gotcha! :)

 

 

I have a problem about deleting images. I created 2 folders, 1st the folder IMAGES wherein the actual size of the images uploaded stored here. The other folder named THUMB stored the images i set the size use for my thumbnails in picture gallery. The problem is that, i dont know how to delete the images both folders. Let say for example, im going to browse the image then when i click delete the image deleted automatically in both folders. (Both folders is the same filename of the images inside)

 

thanks

Link to comment
Share on other sites

i dont know how to delete the images in both folders at the same time.. pls help em.. thanks

 

Folders:

 

1. IMAGES: pic.jpg

2. THUMB : pic.jpg

 

Im going to browse the image after that if im going to click delete the images pic.jpg in folder IMAGES and THUMBS deleted automatically. How can i do that? :)

 

 

 

 

 

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.