Jump to content

Reading a book and lost on how to make these scripts DIRECTLY from the book work


Khoroshko

Recommended Posts

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
foreach($_REQUEST as $key => $val) {
echo $key, " : ", $val, "<br />";
}
} else {
?>
<form action="test.php" method="post">
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="email">Email:</label>
<input type="text" name="email" />
<input type="submit" value="Register!" />
</form>
<?php } // End else statement ?>
 
------------
 
 
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Checks if a file was uploaded without errors
if(isset($_FILES['photo'])
&& is_uploaded_file($_FILES['photo']['tmp_name'])
&& $_FILES['photo']['error']==UPLOAD_ERR_OK) {
// Outputs the contents of $_FILES
foreach($_FILES['photo'] as $key => $value) {
echo "$key : $value <br />";
}
} else {
echo "No file uploaded!";
}
} else {
// If the form was not submitted, displays the form HTML
?>
<form action="test.php" method="post"
enctype="multipart/form-data">
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" value="Upload a Photo" />
</form>
<?php } // End else statement ?>
 
--------------
 
 
<?php
// Checks if the form was submitted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Checks if a file was uploaded without errors
if(isset($_FILES['photo'])
&& is_uploaded_file($_FILES['photo']['tmp_name'])
&& $_FILES['photo']['error']==UPLOAD_ERR_OK) {
// Checks if the file is a JPG image
if($_FILES['photo']['type']=='image/jpeg') {
$tmp_img = $_FILES['photo']['tmp_name'];
// Creates an image resource
$image = imagecreatefromjpeg($tmp_img);
// Tells the browser what type of file
header('Content-Type: image/jpeg');
// Outputs the file to the browser
imagejpeg($image, '', 90);
// Frees the memory used for the file
imagedestroy($image);
} else {
echo "Uploaded file was not a JPG image.";
}
} else {
echo "No photo uploaded!";
}
} else {
// If the form was not submitted, displays the form HTML
?>
 
<form action="test.php" method="post"
enctype="multipart/form-data">
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" value="Upload a Photo" />
</form>
<?php } // End else statement ?>
-----------------
Reading: PHP for the Absolute Beginner
Using dreamweaver cs6 and xampp 
The first thing is a submit form to create an account in the book. I copied it exactly from the book at this point and it still does not work.  I get this when I submit the form on localhost "
Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404 10.0.0.3
Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.1"
"
The book is no help.
Second thing is the first part about image uploading, third is the second bit about it. I ended up copying exactly from the book to see if my typing (which was the exact same) did not work. I get on chrome - a little image of a paper with a broken edge, I guess broken file or no file? Firefox - "No file uploaded! No photo uploaded!" They say that it should work in the book. Can someone help?
I'm so new to PHP that all my looking on the internet was just looking at things I'm barely familiar with. 
Link to comment
Share on other sites

Also if I put a .png file in the submit photo box, it returns this name : acc323.png 

type : image/png 
tmp_name : C:\xampp\tmp\php7224.tmp 
error : 0 
size : 3675 
Uploaded file was not a JPG image. 

 

like it should. so why not show the image?

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.