PDA

View Full Version : File Uploader


Jaynesh
01-25-2006, 03:50 PM
Im learning PHP and came up with this script from a tutorial..


<?php
if(isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE) {
die('Error');
}
if($_FILES['file']['error'] != UPLOAD_ERR_OK) {
die('Error Uploading');
}

$path = '/home/vekaria/public_html/imagehosting/';

$file = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['name'], $newfile);

die('Done');
?>


It is a file uploader,

How can i make it so when i upload an image it will display the image after it has been uploaded.

Cole2026
01-26-2006, 01:05 AM
<?php
if(isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE)
{
die('Error');
}
if($_FILES['file']['error'] != UPLOAD_ERR_OK)
{
die('Error Uploading');
}

$path = '/home/vekaria/public_html/imagehosting/';

$file = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $file);

// get ext
$ext = substr(strrchr($_FILES['file']['name'], '.'), 1);

// make img array
$imgexts = array('jpg','jpeg','gif','png','jfif','bmp');

if ( in_array($ext, $imgexts) )
{
die('<img src="' . $path . $_FILES['file']['name'] . '" />');
}
else
{
die('Done');
}
?>


Here's something simple you can do.

Jaynesh
01-26-2006, 06:42 AM
Im getting this error :(

Parse error: parse error, unexpected T_STRING in /home/vekaria/public_html/learning.php on line 27

Could you please explain what it means and how to fix it?

Cole2026
01-26-2006, 07:25 PM
Im getting this error :(

Parse error: parse error, unexpected T_STRING in /home/vekaria/public_html/learning.php on line 27

Could you please explain what it means and how to fix it?

I was in a rush, forgot to close the die tag.

I updated the tag in my post, it should parse correctly now.

By the way, the "unexpected T_STRING" parsing error usually means you didn't close a function correctly.

Jaynesh
01-27-2006, 03:08 PM
Thanks, worked perfect :)

One last thing :) How can i have it so when an image is uploaded it will also display the link to it aswell.. i tried this ..


<?php
if(isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE)
{
die('Error');
}
if($_FILES['file']['error'] != UPLOAD_ERR_OK)
{
die('Error Uploading');
}

$path = '/home/vekaria/public_html/imagehosting/';
$path2 = 'http://www.lyricalsoundz.com/imagehosting/';

$file = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $file);

// get ext
$ext = substr(strrchr($_FILES['file']['name'], '.'), 1);

// make img array
$imgexts = array('jpg','jpeg','gif','png','jfif','bmp');

if ( in_array($ext, $imgexts) )
{
die('<img src="' . $path2 . $_FILES['file']['name'] . '" />');
}
{
print $path2 . $_FILES['files']['name'];
}
?>


but it only displays the link for anything that is NOT an image, i want it to also apear when an image is uploaded aswell.

Okay ive managed to get it to display the link but having trouble linking the image to do the url.

it just doesnt display when i view the source its there...

<?php
if(isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE)
{
die('Error');
}
if($_FILES['file']['error'] != UPLOAD_ERR_OK)
{
die('Error Uploading');
}

$path = '/home/vekaria/public_html/imagehosting/';
$path2 = 'http://www.lyricalsoundz.com/imagehosting/';

$file = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $file);

// get ext
$ext = substr(strrchr($_FILES['file']['name'], '.'), 1);

// make img array
$imgexts = array('jpg','jpeg','gif','png','jfif','bmp');

if ( in_array($ext, $imgexts) )
{
echo ('<img src="' . $path2 . $_FILES['file']['name'] . '" /> <br />');
}
if ( in_array($ext, $imgexts) )
{
echo ('<a href="' . $path2 . $_FILES['file']['name'] . '" >');
}

?>


Okay my mistake, i fixed it and its working :) wow im getting good at this.

here it is..


<?php
if(isset($_FILES['file']) == false OR $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE)
{
die('Error');
}
if($_FILES['file']['error'] != UPLOAD_ERR_OK)
{
die('Error Uploading');
}

$path = '/home/vekaria/public_html/imagehosting/';
$path2 = 'http://www.lyricalsoundz.com/imagehosting/';

$file = $path . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $file);

// get ext
$ext = substr(strrchr($_FILES['file']['name'], '.'), 1);

// make img array
$imgexts = array('jpg','jpeg','gif','png','jfif','bmp');

if ( in_array($ext, $imgexts) )
{
echo ('<img src="' . $path2 . $_FILES['file']['name'] . '" /> <br />');
}
if ( in_array($ext, $imgexts) )
{
echo ('<a href="' . $path2 . $_FILES['file']['name'] . '" >Image URL</a>');
}

?>


Any suggestions to make it shorter ?