Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 30, 2006, 04:14 PM   #1
gavd
macrumors 6502a
 
Join Date: Jan 2006
PHP Question - From a Newbie!

OK, this is a bit of a long post, but it's because I've copied my code in!
I'm learning PHP from a Wrox published book, but I can't get the following code to work.
It works up to the if (move_uploaded_file()) point, but doesn't seem to do anything from there. It's not erroring because the page loads all the HTML code, but the PHP variables hold no values.

Can anyone shed any light?

Thanks

<?php
//connect to the database
$link = mysql_connect("localhost", "*****", "*****")
or die("Could not connect: " . mysql_error());
mysql_select_db("moviesite", $link)
or die (mysql_error());

//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

//upload image and check for image type
//make sure to change your path to match your images directory
//$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
$ImageDir ="/Users/gavdent/Sites/images/";
$ImageName = $ImageDir . $image_tempname;

if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);

switch ($type) {
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
//insert info into image table
$insert = "INSERT INTO images
(image_caption, image_username, image_date)
VALUES
('$image_caption', '$image_username', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is the picture you just uploaded to our servers:</p>
<img src="images/<?php echo $lastpicid . $ext; ?>" align="left">
<strong><?php echo $image_name; ?></strong><br>
This image is a <?php echo $ext; ?> image.<br>
It is <?php echo $width; ?> pixels wide
and <?php echo $height; ?> pixels high.<br>
It was uploaded on <?php echo $today; ?>.
</body>
</html>
gavd is offline   0 Reply With Quote
Old Jan 30, 2006, 04:20 PM   #2
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
FYI: next time use the php tags

PHP Code:
<?php
//connect to the database
$link mysql_connect("localhost""*****""*****")
or die(
"Could not connect: " mysql_error());
mysql_select_db("moviesite"$link
or die (
mysql_error());

//make variables available
$image_caption $_POST['image_caption'];
$image_username $_POST['image_username'];
$image_tempname $_FILES['image_filename']['name'];
$today date("Y-m-d");

//upload image and check for image type
//make sure to change your path to match your images directory
//$ImageDir ="c:/Program Files/Apache Group/Apache2/test/images/";
$ImageDir ="/Users/gavdent/Sites/images/";
$ImageName $ImageDir $image_tempname;

if (
move_uploaded_file($_FILES['image_filename']['tmp_name'], 
$ImageName)) {
//get info about the image being uploaded
list($width$height$type$attr) = getimagesize($ImageName);

switch (
$type) {
case 
1:
$ext ".gif";
break;
case 
2:
$ext ".jpg";
break;
case 
3:
$ext ".png";
break;
default:
echo 
"Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo 
"Please hit your browser's 'back' button and try again.";
}
//insert info into image table
$insert "INSERT INTO images
(image_caption, image_username, image_date)
VALUES
('
$image_caption', '$image_username', '$today')";
$insertresults mysql_query($insert)
or die(
mysql_error());
$lastpicid mysql_insert_id();
$newfilename $ImageDir $lastpicid $ext;
rename($ImageName$newfilename);
}
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is the picture you just uploaded to our servers:</p>
<img src="http://cdn.macrumors.com/vb/images/<?php echo $lastpicid $ext?>" align="left">
<strong><?php echo $image_name?></strong><br>
This image is a <?php echo $ext?> image.<br>
It is <?php echo $width?> pixels wide 
and <?php echo $height?> pixels high.<br>
It was uploaded on <?php echo $today?>.
</body>
</html>
superbovine is offline   0 Reply With Quote
Old Jan 30, 2006, 04:43 PM   #3
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
Can we see the code from the page which I assume as a html form of some kind?
superbovine is offline   0 Reply With Quote
Old Jan 30, 2006, 04:51 PM   #4
gavd
Thread Starter
macrumors 6502a
 
Join Date: Jan 2006
HTML Code

Sure, here it is. Thanks for the PHP tags tip as well. Didn't realise you could do that.
Code:
<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>
<form name="form1" method="post" action="check_image.php" 
    enctype="multipart/form-data">
<table border="0" cellpadding="5">
  <tr>
    <td>Image Title or Caption<br>
      <em>Example: You talkin' to me?</em></td>
    <td><input name="image_caption" type="text" id="item_caption" size="55" 
          maxlength="255"></td>
  </tr>
  <tr>
    <td>Your Username</td>
    <td><input name="image_username" type="text" id="image_username" size="15" 
          maxlength="255"></td>
  </tr>
    <td>Upload Image:</td>
    <td><input name="image_filename" type="file" id="image_filename"></td>
  </tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
  &nbsp;
  <input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>
gavd is offline   0 Reply With Quote
Old Jan 30, 2006, 05:56 PM   #5
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
oh... heh it also the stupid things...

you need to change the permission on the images directory to 767 from terminal

Code:
cd Sites
chmod 767 images
or just cmd click or right the directory then get info and under the ownership & permissions info set it to.

You can read & write
read & write
group:
no access

others read & write
superbovine is offline   0 Reply With Quote
Old Jan 31, 2006, 01:12 AM   #6
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
Quote:
Originally Posted by gavd
It works up to the if (move_uploaded_file()) point, but doesn't seem to do anything from there.
i haven't looked at your code, but it's possible the problem is on the server end.

i'd written some php to upload files to one of my sites and it failed on the move. i went round and round with the hosting company (hostgator.com, and they were actually pretty awesome about keeping at it) and, suddenly, it all worked.

they said they made a change on their end, but repeated attempts to ask what it was didn't net me an answer (the not so awesome part).

for the next few days, sometimes it would work and sometimes it wouldn't. now it mostly works, but sometimes it still fails. and i have no idea why.

work up the smallest test case you can that still fails and see if your hosting company can reproduce the problem.
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Jan 31, 2006, 01:19 AM   #7
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
if this helps, here's my small test case. you'll have to edit the php file to match your particular file destination.

Code:
<html>
<head>
<title>upload test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form enctype="multipart/form-data" action="upload_test.php" method="post">
        <hidden name="MAX_FILE_SIZE" value=15000 />
        <input name="user_file" type="file" />
        <br>

        <input type="submit" value="Submit" />
</form>

</body>
</html>


PHP Code:
<html>
<head>
<title>upload test php</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<? 
//----------------------------------------------------------------
//
// %Z%%M% %I% %G%
//
// upload_test
//
// sxz 041219
//
//----------------------------------------------------------------

/*
session_start();
header("Cache-control: private");
*/
?>

<body>
<br />

<?php
        
//-----------------------------------------------------------
        // rejectFile
        //-----------------------------------------------------------

        
function rejectFile($reason)
        {
?>
                <h3>
                        Failure
                </h3>
                <p>
<?php
                        
echo $reason;
                        exit();
?>
                </p>
<?php
        
}

        
//-----------------------------------------------------------
        // baseMusicPath
        //-----------------------------------------------------------

        
function baseMusicPath()
        {
                return 
"members/upload_test";
        }

        
//-----------------------------------------------------------
        // wholeMusicPath
        //-----------------------------------------------------------

        
function wholeMusicPath()
        {
                return 
$_SERVER['DOCUMENT_ROOT'] . "/" baseMusicPath();
        }

        
//-----------------------------------------------------------
        // processUploadedFile
        //-----------------------------------------------------------

        
function processUploadedFile($orig_filename$tmp_filename)
        {
                
// ensure uploaded file is good

                
if (!is_uploaded_file($tmp_filename))
                {
                        
rejectFile("Danger: uploaded file " $tmp_filename
                                
" not uploaded correctly");
                        exit;
                }

                
// grab some data so we can determine where to put
                // this thing

                
$path wholeMusicPath();
                
$dst $path "/" $orig_filename;

                
$src $tmp_filename;   // for php library function xfers

                
if (!move_uploaded_file($src$dst))
                {
                        
rejectFile("Could not move " $src " to destination " $dst);
                        exit;
                }

                return 
$dst;
        }

        
//-----------------------------------------------------------
        // main
        //-----------------------------------------------------------

        // grab needed info for our uploaded file

        
$orig_filename $_FILES['user_file']['name'];
        
$tmp_filename $_FILES['user_file']['tmp_name'];

/*
echo "orig_filename : " . $orig_filename; newline();
echo "tmp_filename : " . $tmp_filename; newline();
*/

        
echo "attempting to move " $orig_filename " from " $tmp_filename;
        echo 
"<br><br>";

        
$url processUploadedFile($orig_filename$tmp_filename);

        
$msg "File " $orig_filename " uploaded to " $url;
?>

<h3>
        Success
</h3>
<p>
        <?php echo $msg?>
</p>

</body>
</html>
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Jan 31, 2006, 12:19 PM   #8
gavd
Thread Starter
macrumors 6502a
 
Join Date: Jan 2006
Progress!

OK, thanks to superbovine I can now upload the image to the images folder within my sites folder.

However, the html which should then display the image doesn't!

I have also tried to load the image using preview from the directory, but I get the following message...

Quote:
5.jpg
Couldn't open the file. It may be corrupt or a file format that Preview doesn't recognize.
Anyone got any ideas?

Thanks for your help so far!
gavd is offline   0 Reply With Quote
Old Jan 31, 2006, 01:27 PM   #9
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
download the image and see if you can open it on your mac. or even run diff on it. if it's changed, then you know something happened when it was uploaded or moved.

if it's not changed, then you can start looking at the browser display technology.
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Jan 31, 2006, 09:13 PM   #10
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
if your image over 8 meg that is the default limit for move_uploaded_file.
superbovine is offline   0 Reply With Quote
Old Feb 3, 2006, 03:19 PM   #11
gavd
Thread Starter
macrumors 6502a
 
Join Date: Jan 2006
The images I've been using to test the code are less than 100K so it's not the size that's the issue.
I'm not sure if it could be the renaming of the image which might cause the problem. Not sure what else would cause it to corrupt and not let me be able to open it.
gavd is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
[php] - php problems os x mountain lion 10.8.2 cakas6933 OS X 10.8 Mountain Lion 4 Jun 10, 2014 08:08 AM
Question about php (conditions) olup Web Design and Development 8 May 31, 2013 01:02 AM
newbie with question..... frost5656 iPhone 1 Sep 14, 2012 09:02 PM
Time Capsule - Newbie Question Wifi Question Alper1234 Mac Peripherals 1 Jun 23, 2012 09:17 AM

Forum Jump

All times are GMT -5. The time now is 10:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC