/    Sign up×
Community /Pin to ProfileBookmark

Uploading Pictures

can anyone please have anyways how to upload pictures or where to find codes for that

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@DJRobThaManJun 18.2006 — I actually am working on a projectr where I have to do the same. Here is my code:

[code=php]
<html>
<head>
<title>

<?php

$files = glob("images/*.*");
$i = 0;

while($files[$i])
{
if(substr(strstr($files[$i], "/"), 1) == $_FILES['pic']['name'])
{
?>
Upload Failed
</title>
</head>

<body>


There is already a file on the server with the name <?php echo $_FILES['pic']['name']; ?>. Please rename your file and try again.
<div style="float: right;">
<a title "Click here to view a list of files on the server for reference" href="images.php">List of files on server</a>
</div>
<?php
exit();
}

$i++;
}

if(!empty($_FILES['pic']['name']))
{
?>
Uploading File
</title>
<script type="text/javascript">
<!--

function sendInfo()
{

var pic = "images/<?php echo str_replace(' ', '_', $_FILES['pic']['name']); ?>";
var imgInfo = "<?php echo str_replace('"', '"', $_POST["text"]); ?>";
var start = <?php echo $_POST["start"]; ?>;
var info = opener.document.getElementById("post").value;

if(document.selection)
{
opener.sel.text = "<img alt="" + imgInfo + "" src="" + pic + "">";
}
else
{
opener.document.getElementById("post").value = info.substring(0, start) + "<img alt="" + imgInfo + "" src="" + pic + "">" + info.substring(start, info.length);
}
}

-->
</script>
</head>

<body>

<?php
echo "Uploading file...";
$fup = move_uploaded_file($_FILES['pic']['tmp_name'], "images/" . str_replace(' ', '_', $_FILES['pic']['name']));

if($fup)
{
echo "<br>" . $_FILES['pic']['name'] . " has been successfully uploaded.";
}
else
{
echo "<br>The file <b>" . $_FILES['pic']['name'] . "</b> failed to upload. Please try again.";
}
?>
<div style="float: right;">
<a title="Close window" onClick="<?php
if($fup)
{
echo "sendInfo(); ";
}
?>javascript:window.close();" href=""><?php
if($fup)
{
echo "Apply Changes to the Main Window";
}
else
{
echo "Close Window";
}
?></a>
</div>
<?php
}
else
{
?>
Uploading File
</title>
</head>

<body>

An error has occurred with the upload of the file <b><?php echo $_FILES['pic']['name']; ?></b>. Please try again.
<?php
}

?>

</body>
</html>
[/code]


I've bolded the most important part I think (the part where the php code actually takes the picture from the user's PC and uploads it to the server). Everything else is error catching and a little formatting, as well as adding text to a textarea in another window (that's there because in my project I have a new pop-up with this code appear for the upload and then after the image is placed on the server I insert code into the textarea of the previous window).

I haven't checked this code in a while, but I remember it working the last time I used it. Let me know if it works out for you.

Douglas

PS Okay, so the bold tags don't work within the php tags. So here is the excerpt I was talking about.

[code=php]
<?php
echo "Uploading file...";
$fup = move_uploaded_file($_FILES['pic']['tmp_name'], "images/" . str_replace(' ', '_', $_FILES['pic']['name']));

if($fup)
{
echo "<br>" . $_FILES['pic']['name'] . " has been successfully uploaded.";
}
else
{
echo "<br>The file <b>" . $_FILES['pic']['name'] . "</b> failed to upload. Please try again.";
}
?>
[/code]


One more edit:

In $_FILES['pic']['name']... "pic" is the name of the file input in the form where the user selects which image to upload
Copy linkTweet thisAlerts:
@bokehJun 18.2006 — [URL=http://www.webdeveloper.com/forum/showthread.php?t=101466]Have you read the sticky?[/URL]
Copy linkTweet thisAlerts:
@DJRobThaManJun 18.2006 — haha.. yeah.. that might just help as well
×

Success!

Help @franknu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...