this is error message I'm trying to fix the copy function does not run. This my code:
<?php
if( $_FILES['file']['name'] != "" ) {
copy( $_FILES['file']['name'], "php5/image" ) or die( "Could not copy file!");
} else {
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
Why it doesn't work? How to fix it?
I'm getting
Warning: copy(feel_like_doraemon.jpg): failed to open stream: No such file or directory
When uploading files you should use move_uploaded_file
to actually store the file in your chosen directory. Also, using a full path rather than a relative path has always worked better for me - change the applicable line to suit your environment.
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES['file'] ) ){
try{
$obj=$_FILES['file'];
$name=$obj['name'];
$tmp=$obj['tmp_name'];
$dir=$_SERVER['DOCUMENT_ROOT'] . '/php5/image';
if( !realpath( $dir ) )throw new Exception( sprintf( 'unable to find target directory %s', $dir ) );
$target="{$dir}/{$name}";
if( is_uploaded_file( $tmp ) && move_uploaded_file( $tmp, $target ) ){
/* OK */
@unlink( $tmp );
} else {
/* error */
}
}catch(Exception $e ){
exit( $e->getMessage() );
}
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments