PHP - 文件图像无法上传?

时间:2018-05-16 23:56:08

标签: php image

我正在尝试上传图片,它表示成功,但它不会将文件移动到上传文件夹中。

代码:

<Style TargetType="controls:DropShadowPanel" x:Key="MasterMenuDropShadow">
  <Setter Property="Color" Value="#595959"/>
  <Setter Property="OffsetX" Value="-2"/>
  <Setter Property="OffsetY" Value="0"/>
  <Setter Property="BlurRadius" Value="50"/>
  <Setter Property="ShadowOpacity" Value=".7"/>
</Style>

<Style TargetType="Button" x:Key="MenuButton">
  <Setter Property="Width" Value="{StaticResource AppBarThemeCompactHeight}" />
  <Setter Property="Height" Value="{StaticResource AppBarThemeCompactHeight}" />
  <Setter Property="BorderBrush" Value="Transparent" />
  <Setter Property="BorderThickness" Value="0" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Grid Margin="0">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="PointerOver">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="icon">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Assets\hamburger_onhover.icon.png"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="icon">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="Assets\hamburger.icon.png"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled" />
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Image Name="icon" Source="Assets\hamburger.icon.png" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

表格代码:

if(isset($_POST['uploadImage'])){
    $file = $_FILES['profileIage'];

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.', '$fileName');
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('png', 'jpg', 'jpeg', 'PNG', 'JPG', 'JPEG');

    if(!$fileError == 0){
        echo "There was an error uploading this image!";
    }elseif($fileSize > 2000){
        echo "File is too big!";
    }else{
        $fileNameNew = uniqid('', 'true').".".$fileActualExt;
        $fileDestination = "uploads/".$fileNameNew;
        move_uploaded_file($fileNameNew, $fileDestination);
        echo "Success";
    }
}

谢谢, 乙!

2 个答案:

答案 0 :(得分:0)

试试这个:

if(isset($_POST['uploadImage'])){
    $myFile = $_FILES['profileImage'];

    $fileName = $myFile['name'];
    $fileTmpName = $myFile['tmp_name'];
    $fileSize = $myFile['size'];
    $fileError = $myFile['error'];
    $fileType = $myFile['type'];

    $fileExt = explode('.', '$fileName');
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('png', 'jpg', 'jpeg', 'PNG', 'JPG', 'JPEG');

    if(!$fileError == 0){
        echo "There was an error uploading this image!";
    }elseif($fileSize > 2000){
        echo "File is too big!";
    }else{
        $fileNameNew = uniqid('', 'true').".".$fileActualExt;
        $fileDestination = "uploads/".$fileNameNew;
        move_uploaded_file($fileTmpName, $fileDestination);
        echo "Success";
    }
}

如果要将文件数据存储在变量中,则应使用它。

在此处查看更多内容:http://php.net/manual/en/features.file-upload.php

答案 1 :(得分:0)

如果您进行一些调整

,您的代码将有效
if(isset($_POST['uploadImage'])){
$file = $_FILES['profileImage'];

$fileName = $_FILES['profileImage']['name'];
$fileTmpName = $_FILES['profileImage']['tmp_name'];
$fileSize = $_FILES['profileImage']['size'];
$fileError = $_FILES['profileImage']['error'];
$fileType = $_FILES['profileImage']['type'];

$fileExt = explode('.', '$fileName');
$fileActualExt = strtolower(end($fileExt));

$allowed = array('png', 'jpg', 'jpeg', 'PNG', 'JPG', 'JPEG');

if(!$fileError == 0){
    echo "There was an error uploading this image!";
} else if($fileSize > 2000){
    echo "File is too big!";
} else {
    $fileNameNew = uniqid('', 'true').".".$fileActualExt;
    $fileDestination = "uploads/".$fileNameNew;
    move_uploaded_file($fileNameNew, $fileDestination);
    echo "Success";
}

}

将您的['file']更改为['profileImage']