Jump to content

Recommended Posts

ok so i have the following code:

[code]<?php
if ($_REQUEST[completed] == 1) {
      $source = "/public_html/player_images";
      move_uploaded_file($_FILES['filename']['tmp_name'],
              "../$source/".$_FILES['filename']['name']);
// Following code to fix line ends
      if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) {
      $fi = file("../$source/".$_FILES['filename']['name']);
      $fi2 = fopen("../$source/".$_FILES['filename']['name'],"w");
      foreach ($fi as $lne) {
              $n = rtrim ($lne);
              fputs ($fi2,"$n\n");
              }
      }
//
?>[/code]

and i would like to modify it so it changes the image name to a unique number (a predifined one e.g. account ID) and the size of the image to 250x250 (if it is bigger if not it will stay the size it is). can  someone help me modify the code to do so?
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/
Share on other sites

With regards to the unique name, that's simple, you have the code already.  Instead of calling it the same name as the original file, just use the unique name.

[code]<?php
move_uploaded_file($_FILES['filename']['tmp_name'], "../$source/".$unique_id);
?>[/code]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-145045
Share on other sites

ok so i got it working on an empty page (apart from the error i stated in my other post) but i went to put it into my actual game and now i get these errors when uploading something

[quote]Warning: move_uploaded_file(www.twottk.com/profiles/HPIM0420.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/twottk/public_html/account.php on line 41

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpwa45UE' to 'www.twottk.com/profiles/HPIM0420.jpg' in /home/twottk/public_html/account.php on line 41[/quote]

Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-145088
Share on other sites

[code]<?php
if ($step == 'upload_picture')
        {
       
if ($_REQUEST[completed] == 1) {
      $source = "/public_html/profiles";
      move_uploaded_file($_FILES['filename']['tmp_name'],
              "../$source/".$_FILES['filename']['name']);
// Following code to fix line ends
      if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) {
      $fi = file("../$source/".$_FILES['filename']['name']);
      $fi2 = fopen("../$source/".$_FILES['filename']['name'],"w");
      foreach ($fi as $lne) {
              $n = rtrim ($lne);
              fputs ($fi2,"$n\n");
              }
      }
//
echo "
Your file has been uploaded.";
} else {
echo "This page allows you to upload a file to the demo directory on our
server.<br><br>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Choose file to send: <input type=file name=filename> and
<input type=submit></form><br>";
}
       
        }

?>[/code]

thats it :D
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-146848
Share on other sites

The code seems fine, it must be an issue with the paths...

Why not try setting the path at the beginning as the full path, and using that rather than using relative paths?

[code]<?php
$path = '/home/twottk/public_html/profiles/'; // or whatever the path is
if ($step == 'upload_picture'){
  if ($_REQUEST[completed] == 1){
      move_uploaded_file($_FILES['filename']['tmp_name'], $path.$_FILES['filename']['name']);

      // Following code to fix line ends
      if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) {
        $fi = file($path.$_FILES['filename']['name']);
        $fi2 = fopen($path.$_FILES['filename']['name'],"w");
        foreach ($fi as $lne) {
            $n = rtrim ($lne);
            fputs ($fi2,"$n\n");
        }
      }
  }
}
?>[/code]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147260
Share on other sites

see what im hoping to do is make each image name change to

$Char_id.$image_uploaded.gif/jpg/png/jpeg

which for example would change it to

1.1.gif/jpg/png/jpeg

since this will allow everyone to upload images and the image name wouldnt be the same

but i cant get that to work :P

[code]<?php
if ($step == 'upload_picture')
        {
       
if ($_REQUEST[completed] == 1) {
      $source = "/home/twottk/public_html/profiles/";
      move_uploaded_file($_FILES['filename']['tmp_name'],
              "$source".$char_id.$image_uploaded);
// Following code to fix line ends
      if (! eregi ('(gif|jpg|png)$',$_FILES['filename']['name'])) {
      $fi = file("$source".$_FILES['filename']['name']);
      $fi2 = fopen("$source".$_FILES['filename']['name'],"w");
      foreach ($fi as $lne) {
              $n = rtrim ($lne);
              fputs ($fi2,"$n\n");
              }
      }
//
echo "
Your file has been uploaded.";
} else {
echo "This page allows you to upload a file to the demo directory on our
server.<br><br>
<form enctype=multipart/form-data method=post>
<input type=hidden name=MAX_FILE_SIZE value=150000>
<input type=hidden name=completed value=1>
Choose file to send: <input type=file name=filename> and
<input type=submit></form><br>";
}
       
        }
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-147324
Share on other sites

I spent several days (weeks) trying to do something similar.  Finally got it to working.  It renames and uploads a picture to the server, then adds information including picture name to my data base.  It may be of some help to you.  The code is in my last post at:
http://www.phpfreaks.com/forums/index.php/topic,117897.0.html
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-152541
Share on other sites

^^Thanks for the code but it  doesnt seem to want to get past the "This is a valid file" section of the code.

[code] <?php

if ($step == 'upload_picture')
        {
if ($action == 'submit')
{

if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/jpg"))
{
echo 'That is valid picture file.
';
}
  else
    {
    echo 'There is an error in your file!  Your file must be a jpg or gif format and no larger that 80k';
  exit;
    }
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['file']['name']) ;
//This line assigns a time stapm to a variable. You could also use a random number here if you prefer.


$ran = $char_id.$char_image ;
//This takes the timestamp (or random number) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";
//This assigns the subdirectory you want to save into... make sure it exists!
//find this path using phpinfo.php
$target = "/home/twottk/public_html/profiles/";
//This combines the directory, the random file name, and the extension
$target = $target . $ran2.$ext;
$image = $ran2.$ext;
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{

$sql2="UPDATE accounts SET char_image=$char_image+1 WHERE char_id='$char_id'";
if(mysql_query($sql2))

echo "Image Uploaded Successfully.<br><br>The new iage name is $ran2.$ext<br><br>";
}
else
{
echo 'Sorry, there was a problem uploading your file.';
}

}
echo "<form enctype=\"multipart/form-data\" name=\picture\" action=\"account.php?step=upload_picture&action=submit\" method=\"post\">
<!-- MAX_FILE_SIZE must precede the file input field -->
    <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"82000\" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name=\"file\" type=\"file\" />
 


    <input type=\"submit\" value=\"Send File\" />
</form>";
}

?>[/code]

thats all the code, any help is much apreciated
Link to comment
https://forums.phpfreaks.com/topic/31341-uploading-images/#findComment-155939
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.