/    Sign up×
Community /Pin to ProfileBookmark

Upload images PHP? Oppinions Wanted

Hi,

I have little to no experience with php. upload image programs etc.

I attempted to load a Customer Add Product contribution to an Oscommerce
shop. I managed to get everything working except for the actual upload feature. No matter what I do, I can’t get images to load. I realise there is a well writen tutorial about Image upload in this forum. After posting their I was respectfully asked to relocate my post. Understandable. You would want to keep the tutorial thread clean for questions relating only to the tutorial.

Just the same , I can’t get images to upload. I did use the zip example on this site to test it wasn’t a host or sql problem and it worked fine.

So my first question: Would upload.php which is located in mysite/shop/includes/classes/upload.php be the page that controls the actual upload?

here is my upload.php page. Can anyone detect an error? Or perhaps where I should be inputing information and haven’t?

[CODE]<?php
/*
$Id: upload.php,v 1.2 2003/06/20 00:18:30 hpdl Exp $

osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com

Copyright (c) 2003 osCommerce

Released under the GNU General Public License
*/

class upload {
var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;

function upload($file = ”, $destination = ‘thumb’, $permissions = ‘777’, $extensions = ‘”.jpg”,”.gif”,”.jpeg”,”.png”‘) {

$this->set_file($file);
$this->set_destination($destination);
$this->set_permissions($permissions);
$this->set_extensions($extensions);

$this->set_output_messages(‘direct’);

if (tep_not_null($this->file) && tep_not_null($this->destination)) {
$this->set_output_messages(‘session’);

if ( ($this->parse() == true) && ($this->save() == true) ) {
return true;
} else {
// self destruct
unset($this);//$this = null;

return false;
}
}
}

function parse() {
global $messageStack;

if ((strcasecmp(substr($_FILES[$this->file][‘name’],-4),”.gif”)) && (strcasecmp(substr($_FILES[$this->file][‘name’],-4),”.jpg”)) && (strcasecmp(substr($_FILES[$this->file][‘name’],-4),”.png”)) && (strcasecmp(substr($_FILES[$this->file][‘name’],-4),”.bmp”)) && (strcasecmp(substr($_FILES[$this->file][‘name’],-4),”.swf”))&& (strcasecmp(substr($_FILES[$this->file][‘name’],-4),””))) {
echo “<script>alert(‘The file must be gif, png, jpg, bmp’); window.history.go(-1);</script>n”;
exit();
}
if (isset($_FILES[$this->file])) {
$file = array(‘name’ => $_FILES[$this->file][‘name’],
‘type’ => $_FILES[$this->file][‘type’],
‘size’ => $_FILES[$this->file][‘size’],
‘tmp_name’ => $_FILES[$this->file][‘tmp_name’]);
} elseif (isset($GLOBALS[‘HTTP_POST_FILES’][$this->file])) {
global $HTTP_POST_FILES;

$file = array(‘name’ => $HTTP_POST_FILES[$this->file][‘name’],
‘type’ => $HTTP_POST_FILES[$this->file][‘type’],
‘size’ => $HTTP_POST_FILES[$this->file][‘size’],
‘tmp_name’ => $HTTP_POST_FILES[$this->file][‘tmp_name’]);
} else {
$file = array(‘name’ => (isset($GLOBALS[$this->file . ‘_name’]) ? $GLOBALS[$this->file . ‘_name’] : ”),
‘type’ => (isset($GLOBALS[$this->file . ‘_type’]) ? $GLOBALS[$this->file . ‘_type’] : ”),
‘size’ => (isset($GLOBALS[$this->file . ‘_size’]) ? $GLOBALS[$this->file . ‘_size’] : ”),
‘tmp_name’ => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ”));
}

if ( tep_not_null($file[‘tmp_name’]) && ($file[‘tmp_name’] != ‘none’) && is_uploaded_file($file[‘tmp_name’]) ) {
if (sizeof($this->extensions) > 0) {
if (!in_array(strtolower(substr($file[‘name’], strrpos($file[‘name’], ‘.’)+1)), $this->extensions)) {
if ($this->message_location == ‘direct’) {
$messageStack->add(ERROR_FILETYPE_NOT_ALLOWED, ‘error’);
} else {
$messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, ‘error’);
}

return false;
}
}

$this->set_file($file);
$this->set_filename($file[‘name’]);
$this->set_tmp_filename($file[‘tmp_name’]);

return $this->check_destination();
} else {
if ($this->message_location == ‘direct’) {
$messageStack->add(WARNING_NO_FILE_UPLOADED, ‘warning’);
} else {
$messageStack->add_session(WARNING_NO_FILE_UPLOADED, ‘warning’);
}

return false;
}
}

function save() {
global $messageStack;
list($width, $height, $type, $attr) = getimagesize($this->file[‘tmp_name’]);
//echo $width.’ ‘.$height.’ ‘. $type.’ ‘. $attr;
// die($this->file[‘type’]);

if (substr($this->destination, -1) != ‘/’) $this->destination .= ‘/’;
if (!is_dir($this->destination.’thumb/’)) mkdir ($this->destination.’thumb/’);
if (move_uploaded_file($this->file[‘tmp_name’], $this->destination . $this->filename)) {
copy($this->destination . $this->filename, $this->destination.’thumb/’. $this->filename);
chmod($this->destination . $this->filename, $this->permissions);

$picture = $this->destination . $this->filename; # picture fileNAME here. not address
if (eregi(‘jpeg’,$this->file[‘type’]) || eregi(‘jpg’,$this->file[‘type’]))
{
$function_image_create = “ImageCreateFromJpeg”;
$function_image_new = “ImageJpeg”;
}
if (eregi(‘png’,$this->file[‘type’]))
{
$function_image_create = “ImageCreateFromPng”;
$function_image_new = “ImagePNG”;
}
if (eregi(‘gif’,$this->file[‘type’]))
{
$function_image_create = “ImageCreateFromGif”;
$function_image_new = “ImagePNG”;

}
if (!eregi(‘flash’,$this->file[‘type’]))
{

$src_img=@$function_image_create($picture);
$oh = imagesy($src_img); # original height
$ow = imagesx($src_img); # original width
$r = $oh/$ow;
$new_w = 100;
$new_h = $new_w * $r;
$dst_img = ImageCreateTrueColor($new_w,$new_h);

ImageCopyResized($dst_img, $src_img, 0,0,0,0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img));

@$function_image_new($dst_img, “$picture”);
}

if ($this->message_location == ‘direct’) {
$messageStack->add(SUCCESS_FILE_SAVED_SUCCESSFULLY, ‘success’);
} else {
$messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, ‘success’);
}

return true;
} else {
if ($this->message_location == ‘direct’) {
$messageStack->add(ERROR_FILE_NOT_SAVED, ‘error’);
} else {
$messageStack->add_session(ERROR_FILE_NOT_SAVED, ‘error’);
}

return false;
}
}

function set_file($file) {
$this->file = $file;
}

function set_destination($destination) {
$this->destination = $destination;
}

function set_permissions($permissions) {
$this->permissions = octdec($permissions);
}

function set_filename($filename) {
$this->filename = $filename;
}

function set_tmp_filename($filename) {
$this->tmp_filename = $filename;
}

function set_extensions($extensions) {
if (tep_not_null($extensions)) {
if (is_array($extensions)) {
$this->extensions = $extensions;
} else {
$this->extensions = array($extensions);
}
} else {
$this->extensions = array();
}
}

function check_destination() {
global $messageStack;

if (!is_writeable($this->destination)) {
if (is_dir($this->destination)) {
if ($this->message_location == ‘direct’) {
$messageStack->add(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), ‘error’);
} else {
$messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), ‘error’);
}
} else {
if ($this->message_location == ‘direct’) {
$messageStack->add(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), ‘error’);
} else {
$messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), ‘error’);
}
}

return false;
} else {
return true;
}
}

function set_output_messages($location) {
switch ($location) {
case ‘session’:
$this->message_location = ‘session’;
break;
case ‘direct’:
default:
$this->message_location = ‘direct’;
break;
}
}
}
?>[/CODE]

If this is good. Then what should I be looking at next?

Thank you for any help!!
Nancy ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyMay 13.2006 — To parts.

Part 1.
[code=html]<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>[/code]


Part 2.
[code=php]<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
}
}
?> [/code]
Copy linkTweet thisAlerts:
@bajanboostMay 13.2006 — Hey The Little Guy,

Would your above code (the two parts) allow me to upload files from my harddrive to my ftp?
Copy linkTweet thisAlerts:
@The_Little_GuyMay 14.2006 — This uploads to your host, I don't believe it is an ftp upload. But... This is:

[code=php]
<?php
$connect = ftp_connect("ftp.ispname.com");
$result = ftp_login($connect, "usernam", "password");

if(!$result){
echo "could not connect.";
exit;
}

$result = ftp_put($connect, "newscript.php", "script.php", FTP_ASCII);

if($result){
echo "Sent the file.";
}
else {
echo "Did not send the file.";
}

ftp_close($connect);
?>
[/code]
×

Success!

Help @starynightskyz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...