Jump to content

Image Uploading


benji87

Recommended Posts

Hi all. Im currently working on a CMS for a small school website so they can post their own newsletters. They also want to have the ability to upload images with the newsletter if they wish.

Uploading the image to a directory on the webspace is fine i have that sorted but i also need a link to be inserted into the database where the newsletters are stored so the image will be displayed with it when retrieved.

What ive decided to do is let the user to compose the newsletter first and insert it into the database they then have an option later on to add an image.

I am trying to do this using the $_GET function using id as the unique variable.

Thing is the code seems to exicute but it just wont go into the database i have tried for hours and just can seem to find the solution to the problem.

Here is my code:

actions.php

[code] case 'addimg':

$id = $_GET['id'];
echo ("$id");
if(isset($_POST['Submit']))

{
$size = 500; // the thumbnail height

$filedir = '../images/uploaded/'; // the directory for the original image
$thumbdir = '../images/uploaded/thumbnails/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name

$maxfile = '2000000';
$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];


$sql = mysql_query("UPDATE copnornews SET image = '$userfile_name' WHERE id = '{$_GET['id']}'") or die (mysql_error());

if(!$sql){
echo 'There has been an error while trying to submit your post.';
} else {
$id = mysql_insert_id();
}

if (isset($_FILES['image']['name']))
{
$prod_img = $filedir.$userfile_name;

$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));

$sizes = getimagesize($prod_img);

$aspect_ratio = $sizes[1]/$sizes[0];

if ($sizes[1] <= $size)
{
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size;
$new_width = abs($new_height/$aspect_ratio);
}

$destimg=ImageCreateTrueColor($new_width,$new_height)
or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($prod_img)
or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or die('Problem In resizing');
}
ImageJPEG($destimg,$prod_img_thumb,90)
or die('Problem In saving');
imagedestroy($destimg);
}

echo '
<a href="'.$prod_img.'">
<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'">
</a>';

}else{

echo $id = $_GET['id'];
echo ("$id");
echo '<a href=actions.php?id=$id>ADD IMAGE</a>';

echo '
<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
<input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}[/code]

Now i know the get variable is working so i cant see why it is not working, any help or suggestions on a better way to do it would be great! Thanks
Link to comment
Share on other sites

Well here is the part that sticks out to me:
[code]
if(isset($_POST['Submit'])) --> [b]Here you have post so obviously your sending your variables via a post method[/b]

{
$size = 500; // the thumbnail height

$filedir = '../images/uploaded/'; // the directory for the original image
$thumbdir = '../images/uploaded/thumbnails/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name

$maxfile = '2000000';
$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];


$sql = mysql_query("UPDATE copnornews SET image = '$userfile_name' WHERE id = '{$_GET['id']}'") or die (mysql_error()); -- > [b]And here you are trying to get the id using a get method, try using $_POST instead.[/b]
[/code]
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.