Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need a little coding help
Old 11-01-2008, 05:20 PM Need a little coding help
GamingFreak's Avatar
Super Talker

Posts: 124
Name: Chris
Location: www.DecayedAds.com
Trades: 0
I am trying to make a page of my site where others can use it to upload and host images, just images for right now. I used a tutorial from another site to type it up and Im not too familiar with php. I have it uploaded to my site and the browse files and all work but when I click submit, the page will act as it is loading the upload and at the end it wont upload the image or anything nor give the [img] tags to use. Anyone help me on how to do this?

Here is my coding so far.

PHP Code:
<?php
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","200"); 
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i strrpos($str,".");
if (!
$i) { return ""; }
$l strlen($str) - $i;
$ext substr($str,$i+1,$l);
return 
$ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no error found) 
//and it will be changed to 1 if an errro occures. 
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit'])) 
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image
{
//get the original name of the file from the clients machine
$filename stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension getExtension($filename);
$extension strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, 
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size MAX_SIZE*1024)
{
echo 
'<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied copy($_FILES['image']['tmp_name'], $newname);
if (!
$copied
{
echo 
'<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors
{
echo 
"<h1>File Uploaded Successfully! Try again!</h1>";
}
?>
<!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->
<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td><input type="file" name="image"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table> 
</form>
__________________
FREE LINK DIRECTORY!
Please login or register to view this content. Registration is FREE
GamingFreak is offline
Reply With Quote
View Public Profile Visit GamingFreak's homepage!
 
 
Register now for full access!
Old 11-03-2008, 08:54 PM Re: Need a little coding help
GamingFreak's Avatar
Super Talker

Posts: 124
Name: Chris
Location: www.DecayedAds.com
Trades: 0
Ok..a simple "I dont know" would help rather than not answering!
__________________
FREE LINK DIRECTORY!
Please login or register to view this content. Registration is FREE
GamingFreak is offline
Reply With Quote
View Public Profile Visit GamingFreak's homepage!
 
Old 11-03-2008, 10:35 PM Re: Need a little coding help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by GamingFreak View Post
Ok..a simple "I dont know" would help rather than not answering!
I hope the day never comes when all of the hundreds of active members here feel obligated to post "I don't know" rather than nothing at all.

http://www.w3schools.com/PHP/php_file_upload.asp
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-03-2008, 11:33 PM Re: Need a little coding help
GamingFreak's Avatar
Super Talker

Posts: 124
Name: Chris
Location: www.DecayedAds.com
Trades: 0
Ok, I am able to pick a file, click upload and then it will seem to upload and this is what I get.

Upload: GetAttachment.jpg
Type: image/pjpeg
Size: 48.8486328125 Kb
Stored in: /tmp/phpynZdSh

I cant find the image on the FTP anywhere, where did it upload too?

Also, I want to make it upload to a certain directory inside the ftp, how do i do this?

This is what I have:

PHP Code:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| (
$_FILES["file"]["type"] == "image/jpeg")
|| (
$_FILES["file"]["type"] == "image/pjpeg"))
&& (
$_FILES["file"]["size"] < 20000))
  {
  if (
$_FILES["file"]["error"] > 0)
    {
    echo 
"Return Code: " $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo 
"Upload: " $_FILES["file"]["name"] . "<br />";
    echo 
"Type: " $_FILES["file"]["type"] . "<br />";
    echo 
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo 
"Temp file: " $_FILES["file"]["tmp_name"] . "<br />";    if (file_exists("upload/" $_FILES["file"]["name"]))
      {
      echo 
$_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      
move_uploaded_file($_FILES["file"]["tmp_name"],
      
"upload/" $_FILES["file"]["name"]);
      echo 
"Stored in: " "upload/" $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo 
"File is either too large or invalid type.";
  }
?>
__________________
FREE LINK DIRECTORY!
Please login or register to view this content. Registration is FREE

Last edited by GamingFreak; 11-03-2008 at 11:46 PM..
GamingFreak is offline
Reply With Quote
View Public Profile Visit GamingFreak's homepage!
 
Old 11-04-2008, 12:32 AM Re: Need a little coding help
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Always use absolute paths to avoid stupid errors. I am 99% sure that your "upload/" leads to wrong place.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 11-04-2008, 12:08 PM Re: Need a little coding help
GamingFreak's Avatar
Super Talker

Posts: 124
Name: Chris
Location: www.DecayedAds.com
Trades: 0
I have it fixed, and like I said in the other topic that you so rudely replied, I ask that you do not contact me in any way.

Thanks
__________________
FREE LINK DIRECTORY!
Please login or register to view this content. Registration is FREE
GamingFreak is offline
Reply With Quote
View Public Profile Visit GamingFreak's homepage!
 
Old 11-04-2008, 02:41 PM Re: Need a little coding help
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Quote:
Originally Posted by GamingFreak View Post
I have it fixed, and like I said in the other topic that you so rudely replied, I ask that you do not contact me in any way.

Thanks
Seriously have I missed something? Your asking for help to a problem (Which you were given), and you have not even searched for the solution.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Reply     « Reply to Need a little coding help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46362 seconds with 12 queries